記事のディレクトリ
このブログのデモ元アドレス
https://github.com/suchahaerkang/spring-annotation.git
1つの@Profile役割
@Profile的作用:切换不同的环境,可以动态的将不同的组件注册到容器中去
今、プロジェクトの開発で我々一般には(DEV)、テスト(試験)および生産(PROD)環境を整備する必要があることなどの需要、我々はすべて知っているがあります。各環境のデータ・ソースは、私たちが今だけの環境を切り替え実現方法、各成分を動的にコンテナ、それに登録されますが、同じではありませんか?
書き込みコードから始めましょう
ここでは、プロジェクトに最初に導入されたパケット・データ・ソースを我々はC3P0を使用しています。次に、それをドライバパッケージに導入され、mysqlの
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.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>
コンフィギュレーション・クラスを書き、容器レジスタの3つの成分(ソースデータがdevDataSource、テストデータソースtestDataSourceが開発、データソースprodDataSourceを生成)
/**
* @description:
* @author: sukang
* @date: 2020-03-08 12:40
*/
@PropertySource(value = {"classpath:/db.properties"})
@Configuration
public class MainConfigOfProfile {
@Value("${database.username}")
private String userName;
@Value("${database.password}")
private String password;
@Value("${database.driver}")
private String driver;
@Bean("devDataSource")
public DataSource dataSourceOfDev() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("http://localhost:3306/dev");
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setDriverClass(driver);
return dataSource;
}
@Bean("testDataSource")
public DataSource dataSourceOfTest() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("http://localhost:3306/test");
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setDriverClass(driver);
return dataSource;
}
@Bean("prodDataSource")
public DataSource dataSourceOfProd() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("http://localhost:3306/prod");
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setDriverClass(driver);
return dataSource;
}
}
デフォルトでは、これら3つのデータソースがするコンテナに登録される
テストケースを書きます
@Test
public void test01(){
//创建容器
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
//获取容器中组件为DataSource的所有名字
String[] beanNames = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNames) {
System.out.println(name);
}
}
業績の
テスト結果を見ることができる、3つのデータソースをコンテナに登録されているが、これは我々が望むものではない、我々はあなたがどのような種類のデータソースの登録したい種類の環境のものの中にあります。私たちは@Profileノートでこれを実現してみましょう、プラス@Profile注釈付きクラス構成
/**
* @description:
* @author: sukang
* @date: 2020-03-08 12:40
*/
@PropertySource(value = {"classpath:/db.properties"})
@Configuration
public class MainConfigOfProfile {
@Value("${database.username}")
private String userName;
@Value("${database.password}")
private String password;
@Value("${database.driver}")
private String driver;
@Profile("dev")
@Bean("devDataSource")
public DataSource dataSourceOfDev() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setDriverClass(driver);
return dataSource;
}
@Profile("test")
@Bean("testDataSource")
public DataSource dataSourceOfTest() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setDriverClass(driver);
return dataSource;
}
@Profile("prod")
@Bean("prodDataSource")
public DataSource dataSourceOfProd() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/prod");
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setDriverClass(driver);
return dataSource;
}
}
在配置类中加上@Profile之后,还没有效果,必须在启动启动容器的时候激活某个环境才能生效
図2は、環境をアクティブにするには、2つの方法があります
1)使用命令行动态参数,在虚拟机参数位置加上'-Dspring.profiles.active=dev'
業績
2)另外一种方法是通过写代码的方式
@Test
public void test02(){
//创建容器调用无参构造函数
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//激活测试环境
applicationContext.getEnvironment().setActiveProfiles("test");
//注册MainConfigOfProfile组件
applicationContext.register(MainConfigOfProfile.class);
//刷新容器
applicationContext.refresh();
//获取容器中组件为DataSource的所有名字
String[] beanNames = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNames) {
System.out.println(name);
}
}
業績