轉自:
http://www.java265.com/JavaFramework/Spring/202206/3613.html
下文筆者講述@Profile注解功能說明,如下所示
@Profile:
Profile的功能就是配置
讓應用程式來說,不同的環境需要不同的配置
如:
開發環境,應用需要連接一個可供除錯的資料庫單機行程
生產環境,應用需要使用正式發布的資料庫,通常是高可用的集群
測驗環境,應用只需要使用記憶體式的模擬資料庫
Spring框架提供了多profile的管理功能,我們可以使用profile功能來區分不同環境的配置
例
配置類
package com.java265.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;
/**
* Profile:
* Spring為我們提供的可以根據當前環境,動態的激活和切換一系列組件的功能
*
* 開發環境、測驗環境、生產環境
* 資料源:(/A)、(/B)、(/C)
*
*
* @Profile:指定組件在哪個環境下才能被注冊到容器中,不指定,任何環境都會注冊
*
* 可以寫在類上,只有是指定的環境,該類的所有配置才能開始生效
*/
@Configuration
@PropertySource("classpath:db.properties")
public class MainConfigOfProfile implements EmbeddedValueResolverAware {
@Value("${db.user}")
private String user;
private StringValueResolver valueResolver;
private String driverClass;
@Profile("test")
@Bean("testDataSource")
public DataSource dataSource(@Value("${db.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("dev")
@Bean("devDataSource")
public DataSource dataSourceDev(@Value("${db.password}") String password) throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword(password);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("prod")
@Bean("prodDataSource")
public DataSource dataSourceProd(@Value("${db.password}") String password) throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword(password);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/prod");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
this.valueResolver = stringValueResolver;
this.driverClass = valueResolver.resolveStringValue("${db.driverClass}");
}
}
測驗
@Test
public void test02(){
//創建一個ApplicationContext
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
//設定需要激活的環境(可以多個)
ac.getEnvironment().setActiveProfiles("test", "dev");
//注冊主配置類
ac.register(MainConfigOfProfile.class);
//啟動重繪容器
ac.refresh();
String[] names = ac.getBeanNamesForType(DataSource.class);
for(String name : names){
System.out.println(name);
}
ac.close();
}
---運行以上代碼,將輸出以下資訊------
testDataSource
devDataSource
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/494290.html
標籤:其他
上一篇:Snowflake(雪花演算法),什么情況下會沖突?
下一篇:Laravel控制器查詢
