You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Custom @Profile annotations not working on @Configuration classes [SPR-11808] #16428
Starting at version v3.2.2, custom @Profile annotations no longer work on @Configuration classes.
@Profile("main")
public @interface Main {
}
@Configuration
@ComponentScan(basePackageClasses = MainConfiguration.class)
@Main
public class MainConfiguration {
}
Before version 3.2.3, the above configuration works as expected, MainConfiguration only gets executed when profile "main" is activated.
Unfortunately, when I upgraded to version 3.2.8, whenever I have a custom profile annotation on a @Configuration class, the active profiles are not honored.
I've searched the documentation looking for some light on this, but with no luck.
Is this the expected behavior or a bug?
I've attached a sample application to demonstrate this behavior.
mvn install
Will use version 3.2.2, and all tests must pass.
mvn -Pbug install
will use version 3.2.8, and tests shall fail.
I think I found the commits that changed this behavior, they are related to issue #15176. When I revert the commits related to that issue, everything works for me.
I have a large enterprise application that relies on that feature (custom @Profiles) and we need to upgrade Spring because of issues #15018 and #15726.
Thanks!
The use of custom profile annotations sounds fine to me and should indeed work in the larger Spring meta-annotation vision. That said, I'm afraid this hasn't been properly defined for @Profile before and more or less worked by accident up until 3.2.2... That change in 3.2.3 happened more than a year ago in the meantime, and we haven't had a complaint about it before.
We can turn this into a properly defined feature for Spring Framework 4.1 now, with a GA target in mid July. However, I'm afraid it's a bit late to roll this into the 4.0.x line or a further 3.2.x release at this point since both are at the end of their respective line already. We might do a 3.2.10 at some point but there is no plan for when or whether we'll do that one at all :-(
Actually, I need to take a step back on my comment above with respect to the state of the art: There has been a complete revision of profile annotation handling in the 4.0.x line, and using custom profile annotations is fully supported there already. As for 4.0 GA and in an even further refined variant in 4.0.3, your scenario should work just fine.
Ok. But why the attached project does not work when using any version from 4.0.0.RELEASE to 4.0.5.RELEASE?
mvn clean test -Dversion.org.springframework=4.0.5.RELEASE
After some local variations of the submitted sample app, it seems that the problem is simply the lack of @Retention(RetentionPolicy.RUNTIME) in your custom profile annotation declarations. With that added, a clean build of your sample works against any version of Spring that I've tried... even against 3.2.3. I have no idea why it worked without runtime retention before, but in any case, that retention declaration is necessary in order to guarantee access via reflection at runtime - as clearly needed by Spring.
Activity
spring-projects-issues commentedon May 25, 2014
Juergen Hoeller commented
The use of custom profile annotations sounds fine to me and should indeed work in the larger Spring meta-annotation vision. That said, I'm afraid this hasn't been properly defined for
@Profilebefore and more or less worked by accident up until 3.2.2... That change in 3.2.3 happened more than a year ago in the meantime, and we haven't had a complaint about it before.We can turn this into a properly defined feature for Spring Framework 4.1 now, with a GA target in mid July. However, I'm afraid it's a bit late to roll this into the 4.0.x line or a further 3.2.x release at this point since both are at the end of their respective line already. We might do a 3.2.10 at some point but there is no plan for when or whether we'll do that one at all :-(
Juergen
spring-projects-issues commentedon May 30, 2014
Juergen Hoeller commented
Actually, I need to take a step back on my comment above with respect to the state of the art: There has been a complete revision of profile annotation handling in the 4.0.x line, and using custom profile annotations is fully supported there already. As for 4.0 GA and in an even further refined variant in 4.0.3, your scenario should work just fine.
Juergen
spring-projects-issues commentedon Jun 2, 2014
Miguel A. Baldi Hörlle commented
Ok. But why the attached project does not work when using any version from 4.0.0.RELEASE to 4.0.5.RELEASE?
mvn clean test -Dversion.org.springframework=4.0.5.RELEASE
spring-projects-issues commentedon Jun 3, 2014
Rafael Senna commented
I also tested here with 4.0.5.RELEASE and does not work. Should we reopen the issue?
spring-projects-issues commentedon Jun 3, 2014
Juergen Hoeller commented
Alright, so it seems we've been missing something in our isolated tests there.
Generally speaking,
@Profileas a meta-annotation does work in the 4.0.x line. Your scenario seems to have something subtle that breaks it.In any case, I'll have another look.
Juergen
spring-projects-issues commentedon Jun 5, 2014
Marcelo Borges Ribeiro commented
Same problem here. I'd appreciate any help.
Thanks in advance!
spring-projects-issues commentedon Jun 5, 2014
Juergen Hoeller commented
After some local variations of the submitted sample app, it seems that the problem is simply the lack of
@Retention(RetentionPolicy.RUNTIME) in your custom profile annotation declarations. With that added, a clean build of your sample works against any version of Spring that I've tried... even against 3.2.3. I have no idea why it worked without runtime retention before, but in any case, that retention declaration is necessary in order to guarantee access via reflection at runtime - as clearly needed by Spring.Hope that helps,
Juergen
spring-projects-issues commentedon Jun 5, 2014
Miguel A. Baldi Hörlle commented
Thank you very much, Juergen!