pom文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
主配置类
package cn.jwt.config;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
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 com.mchange.v2.c3p0.ComboPooledDataSource;
//可以根据当前环境,动态的激活和切换一系列组件的功能
//开发环境a,测试环境b,生产环境c
//1 加了环境标识的bean,只有这个环境被激活时才能注册到容器中。默认是default环境
//2 如果写在配置类上,只有指定的环境被激活时,整个配置类里面的所有配置才能生效
//3 没有环境标识的bean,在任何环境下都是加载的
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class MyconfigProfile {
@Value("${username}")
private String user;
@Value("${password}")
private String passwrod;
@Profile("test")
@Bean("dataSourceTest")
public DataSource dataSourceTest(@Value("${url}") String url)throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(passwrod);
dataSource.setJdbcUrl(url);
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
return dataSource;
}
@Profile("pro")
@Bean("dataSourceProduction")
public DataSource dataSourceProduction(@Value("${url}") String url)throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(passwrod);
dataSource.setJdbcUrl(url);
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
return dataSource;
}
@Profile("dev")
@Bean("dataSourceDevelope")
public DataSource dataSourceDevelope(@Value("${url}") String url)throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(passwrod);
dataSource.setJdbcUrl(url);
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
return dataSource;
}
}
测试类
package cn.jwt.app;
import java.util.Arrays;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import cn.jwt.config.MyconfigProfile;
public class AppTestProfile {
//两种方式
//1 使用命令行动态参数,在虚拟机位置加载 -Dspring.profiles.active=test
//2 代码控制
public static void main(String[] args) {
//1 创建IOC容器 appclicationcontext
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
//2 设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("test","dev");
//3 注册主配置类
applicationContext.register(MyconfigProfile.class);
//4 启动刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
System.out.println(Arrays.toString(beanNamesForType));
}
}