Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a Spring profile to a package?

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.

like image 214
JSPDeveloper01 Avatar asked Nov 28 '14 09:11

JSPDeveloper01


People also ask

How do I select a profile in spring boot?

Setting Active Profile Once you have profile specific configuration, you would need to set the active profile in an environment. When you restart the application, you would see that the dev profile is active. You can see that the configuration for dev profile is being picked up by Spring Boot.

How do you define beans for a specific profile?

Bean definition profiles are a mechanism by which application context is configured differently for different environments. You group bean definitions under named profiles in XML or using annotation and activate one or more profiles in each environment.


1 Answers

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 {
}
like image 77
Jan Zyka Avatar answered Oct 11 '22 17:10

Jan Zyka