Skip to content

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

Closed

Description

@spring-projects-issues

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

Activity

  1. spring-projects-issues commented on May 25, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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

  2. spring-projects-issues commented on May 30, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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

  3. spring-projects-issues commented on Jun 2, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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

  4. spring-projects-issues commented on Jun 3, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rafael Senna commented

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

  5. spring-projects-issues commented on Jun 3, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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

  6. spring-projects-issues commented on Jun 5, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Marcelo Borges Ribeiro commented

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

  7. spring-projects-issues commented on Jun 5, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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

  8. spring-projects-issues commented on Jun 5, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Miguel A. Baldi Hörlle commented

    Thank you very much, Juergen!

  9. added
    type: bug
    status: invalid
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: invalid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions