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

Custom @Profile annotations not working on @Configuration classes [SPR-11808] #16428

Closed
spring-projects-issues opened this issue May 20, 2014 · 8 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented May 20, 2014

Miguel A. Baldi Hörlle opened SPR-11808 and commented

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!


Affects: 3.2.3, 3.2.8

Attachments:

Issue Links:

Referenced from: commits 5eecb13

4 votes, 8 watchers

@spring-projects-issues
Copy link
Collaborator Author

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 @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 :-(

Juergen

@spring-projects-issues
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

Rafael Senna commented

I also tested here with 4.0.5.RELEASE and does not work. Should we reopen the issue?

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Alright, so it seems we've been missing something in our isolated tests there.

Generally speaking, @Profile as 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
Copy link
Collaborator Author

Marcelo Borges Ribeiro commented

Same problem here. I'd appreciate any help.
Thanks in advance!

@spring-projects-issues
Copy link
Collaborator Author

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
Copy link
Collaborator Author

Miguel A. Baldi Hörlle commented

Thank you very much, Juergen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants