<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<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>5.1.44</version>
</dependency>
dataSource.user=root
dataSource.password=123
dataDriveClassName=com.mysql.jdbc.Drive
package com.spring.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
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 javax.sql.DataSource;
import java.beans.PropertyVetoException;
// 加载配置文件
@PropertySource("classpath:/dataSource.properties")
@Configuration
public class TestProfileConfig implements EmbeddedValueResolverAware {
// 通过@Value注解获取配置文件dataSource.user的值
@Value("${dataSource.user}")
private String user;
private StringValueResolver resolver;
private String dirveClassName;
/**
* 开发环境
**/
@Profile("dev")
@Bean
public DataSource dataSourceDev(@Value("${dataSource.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev_db");
dataSource.setDriverClass(dirveClassName);
return dataSource;
}
/**
* 测试环境
**/
@Profile("test")
@Bean
public DataSource dataSourceTest(@Value("${dataSource.password}") String pwd) throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test_db");
dataSource.setDriverClass(dirveClassName);
return dataSource;
}
/**
* 生产环境
**/
@Profile("onLine")
@Bean
public DataSource dataSourceOnLine(@Value("${dataSource.password}") String pwd) throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/online_db");
dataSource.setDriverClass(dirveClassName);
return dataSource;
}
/**
* 通过StringValueResolver解析dataDriveClassName的值
**/
public void setEmbeddedValueResolver(StringValueResolver resolver) {
dirveClassName=resolver.resolveStringValue("${dataSource.dataDriveClassName}");
}
}
package com.spring.test;
import com.spring.config.TestProfileConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class TestProfile {
public static void main(String[] args) {
/* 命令行动态参数:运行的时候给虚拟机参数位置增加 -Dspring.profiles.active=dev
通过代码方式控制:
1首先创建一个AnnotationConfigApplicationContext
2 设置环境变量,指定要激活的环境
3 注册配置类
4启动的时候刷新容器
*/
// 01 首先创建一个AnnotationConfigApplicationContext
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//02 设置环境变量,指定要激活的环境 可以指定一个或者多个
context.getEnvironment().setActiveProfiles("dev","onLine");
//03 注册配置类
context.register(TestProfileConfig.class);
//04 启动刷新容器
context.refresh();
String[] names = context.getBeanNamesForType(DataSource.class);
for (String name : names) {
System.out.println(name);
}
}
}
IT技术分享社区
个人博客网站:https://programmerblog.xyz
联系客服