How to install Spring profile in a package?

I want to set the profile name for the whole package, and I don't know how to do it. If where there is no easy way, I should mark every class in the package and subpackages with the @Profile annotation.

Tag

<context:component-scan/> does not support an attribute of type profile , so I have no idea.

+4
source share
2 answers

You can configure a profile for:

  • Spring Beans XML file - for xml configuration
 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" profile="your-profile"> <context:component-scan base-package="your.package" /> </beans> 
  • @Configuration class for Java configuration
 @Configuration @Profile("your-profile") @Componentscan("your.package") class AppConfig { } 

In each of them, you can use component scanning for your specific package.

+2
source

If you are not mixing XML and Java configuration, can you use @Profile in a bean that can contain the @ComponentScan annotation with your target package?

Similarly with XML: you can have two different <beans ...> sections, each with a different profile, and in each 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 { } 
+1
source

Source: https://habr.com/ru/post/1261583/


All Articles