How to set a Spring profile to a package?
Asked Answered
N

1

6

I want to set a profile name to a whole package and I don't know how. If where is no easy way then I have to mark every class in the package and sub packages with @Profile annotation.

<context:component-scan/> tag does not support attribute like profile so I have no idea.

Netsuke answered 28/11, 2014 at 9:59 Comment(0)
C
5

If you don't mix XML and Java config you could use @Profile on a bean which has @ComponentScan annotation with your target package maybe?

Similarly with XML: you can have two different <beans ...> sections, each with different profile and in each of the section you define your own <context:component-scan basePackage="..." />

@Configuration
@Profile("profile1")
@ComponentScan(basePackage="package1")
class Config1 {
}

@Configuration
@Profile("profile2")
@ComponentScan(basePackage="package2")
class Config2 {
}
Colonialism answered 28/11, 2014 at 10:10 Comment(2)
And, we can specify active profile in the application.properties: spring.profiles.active=profile1Archean
it does not work with spring boot and with a NOT profile, i.e. @Profile("!dev")Olid

© 2022 - 2024 — McMap. All rights reserved.