How to set a Spring profile to a package?

前端 未结 2 1782
广开言路
广开言路 2021-01-19 16:31

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 @Pr

相关标签:
2条回答
  • 2021-01-19 16:48

    You can set 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 config
    @Configuration
    @Profile("your-profile")
    @Componentscan("your.package")
    class AppConfig {
    }
    

    In each of them you can use component scanning for your a particular package.

    0 讨论(0)
  • 2021-01-19 16:59

    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 {
    }
    
    0 讨论(0)
提交回复
热议问题