Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: @PropertySource meta-annotation not detected next to direct annotation [SPR-16592] #21134

Closed
spring-projects-issues opened this issue Mar 14, 2018 · 2 comments
Assignees
Labels
in: core status: backported type: task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 14, 2018

Evgeny Naku opened SPR-16592 and commented

Check out sample bug project on github: https://github.com/0v1se/spring-properties-bug

@PropertySource is not used when it's defined in other annotation.

For example, I have

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@PropertySource("app1.properties")
public @interface IncludeApp1Properties {
}

Then I have configuration:

@Configuration
@IncludeApp1Properties
@PropertySource("app2.properties")
public class ErrorConfiguration {
    @Value("${intValue1}")
    private int value1;
    @Value("${intValue2}")
    private int value2;
}

When I try to use ErrorConfiguration, there is an error Caused by: java.lang.NumberFormatException: For input string: intValue1

But when I try to use @PropertySource directly, everything is fine:

@Configuration
@PropertySource("app1.properties")
@PropertySource("app2.properties")
public class OkConfiguration {
    @Value("${intValue1}")
    private int value1;
    @Value("${intValue2}")
    private int value2;
}

Affects: 4.3.14, 5.0.3

Reference URL: https://github.com/0v1se/spring-properties-bug

Backported to: 4.3.15

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 14, 2018

Juergen Hoeller commented

I'm afraid this is a known limitation: A direct @PropertySource annotation effectively overrides any meta-annotations. The only way to apply multiple property source annotations is to declare several @PropertySource annotations at the same level (directly on the same class or all on the same custom annotation), or to split your custom @PropertySource annotations onto several configuration classes (one each).

In any case, we have to document this, so I've turned this into a documentation ticket.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 15, 2018

Evgeny Naku commented

Thanks for documenting this.

Just a note:
Unfortunately, the same applies for other repeated annotations. For example, @ComponentScan.
So, I can't have something like this:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan
.....
public @interface EnableDatabase {
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
    Class<?>[] scanPackage() default {};
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan
.....
public @interface EnableController {
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
    Class<?>[] scanPackage() default {};
}

@EnableDatabase(scanPackage = Database.class)
@EnableController(scanPackage = Controller.class)
public class Configuration {
}

Spring will scan for components only in one place, not in both.
So I have to divide Configuration to two: DatabaseConfiguration and ControllerConfiguration. Then it'll work

@spring-projects-issues spring-projects-issues added status: backported in: core type: task labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0.5 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: backported type: task
Projects
None yet
Development

No branches or pull requests

2 participants