spring中@Profile的作用


根據系統環境的不同,Profile可以用來切換數據源。例如切換開發,測試,生產環境的數據源。

舉個例子:

先創建配置類MainProfileConfig:

@Configuration
@PropertySource("classpath:/jdbc.properties")
public class MainProfileConfig implements EmbeddedValueResolverAware {

    @Value("${db.user}")
    private String user;

    private String driverClass;

    private StringValueResolver stringValueResolver;

    @Profile("test")
    @Bean("testDataSource")
    public  DataSource getTestDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(pwd);
        comboPooledDataSource.setDriverClass(driverClass);
        return comboPooledDataSource;
    }

    @Profile("dev")
    @Bean("devDataSource")
    public  DataSource getDevDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(pwd);
        comboPooledDataSource.setDriverClass(driverClass);
        return comboPooledDataSource;
    }

    @Profile("pro")
    @Bean("proDataSource")
    public  DataSource getproDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(pwd);
        comboPooledDataSource.setDriverClass(driverClass);
        return comboPooledDataSource;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        this.stringValueResolver = stringValueResolver;
        driverClass = stringValueResolver.resolveStringValue("${db.driverClass}");
    }
}

這里使用@Value和StringValueResolver來給屬性賦值

 

測試:運行的時候設置環境   -Dspring.profiles.active=dev

 

 

 

public class ProFileTest {

    @Test
    public void  test(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainProfileConfig.class);

        String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
        for (String name : beanNamesForType){
            System.out.println(name);
        }
        applicationContext.close();
    }
}

打印輸出:

devDataSource

 

也可以使用代碼的方式設置系統環境,創建容器的時候使用無參構造方法,然后設置屬性。

@Test
    public void  test(){
        //創建容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //設置需要激活的環境
        applicationContext.getEnvironment().setActiveProfiles("test");
        //設置主配置類
        applicationContext.register(MainProfileConfig.class);
        //啟動刷新容器
        applicationContext.refresh();

        String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
        for (String name : beanNamesForType){
            System.out.println(name);
        }
        applicationContext.close();
    }

打印輸出:

testDataSource

setActiveProfiles設置環境的時候可以傳入多個值,它的方法可以接受多個參數。
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
    void setActiveProfiles(String... var1);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM