spring @Profile根据环境注册bean

1、配置文件类

package com.zdc.sp.autowire.config;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * Profile 
 *         Spring 为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能
 * 
 * 1)、加了环境标识的bean,只有这个环境被激活的时候才能被注册到容器中。默认是default环境
 *     
 * 2)若写在配置类上,只有指定的环境的时候,整个配置类里面的所有配置才能生效
 *
 */
@PropertySource("classpath:/profile.properties")
@Configuration
public class ProfileConfig implements EmbeddedValueResolverAware{

    @Value("${user}")
    private String user;
    
    private StringValueResolver valueResolver;
    
    private String driverClass;
    
    @Profile("default")
    @Bean
    public DataSource dataSourcetest(@Value("${password}") String password) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }
    
    
    @Profile("pro")
    @Bean
    public DataSource dataSourcepro() throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword("root");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }
    
    @Profile("dev")
    @Bean
    public DataSource dataSourcedev() throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword("root");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

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

2、测试类:

package com.zdc.sp.autowire.test;


import javax.sql.DataSource;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.zdc.sp.autowire.config.ProfileConfig;

public class ProFileTest {

    public static void main(String[] args) {
        //1、使用命令动态参数:在虚拟机参数位置加载-Dspring.profiles.active=test
        //AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProfileConfig.class);
        //代码激活某种环境
        
        //、创建一个applicationContext
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //2、设置所需要的环境
        context.getEnvironment().setDefaultProfiles("dev");
        //3、注册配置类
        context.register(ProfileConfig.class);
        //4、启动刷新容器
        context.refresh();
        String[] namesType = context.getBeanNamesForType(DataSource.class);
        for (String string : namesType) {
            
            System.out.println("@@@@@@@@@@@@@:"+string);
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/u014450465/article/details/89527463