此概念是針對不同部署環境的輕松配置區分符。
直接的用例(已提出)是對相關類進行注釋,以便Spring根據活動的配置文件加載適當的類。
但是,這種方法可能并不總是適用于常見的用例……通常,配置密鑰是相同的,并且僅值會隨環境而變化。
在本文中,我想提出一種模式來支持按環境加載配置數據, 而無需為每個概要文件(即針對每個環境)創建/維護多個類。
在整個文章中,假設每個部署環境的數據庫定義(例如用戶名或連接URL)不同,我將以數據庫連接配置為例。
主要思想是使用一個類來加載配置(即,一個類用于數據庫連接定義),然后將包含正確概要文件配置數據的適當實例注入其中。
為了方便和清楚起見,該過程分為三個階段:
階段1 :基礎設施
步驟1.1 –創建一個包含所有配置數據的屬性文件
步驟1.2 –為每個配置文件創建注釋 步驟1.3 –確保在上下文加載期間加載了配置文件
階段2 :實施配置文件模式
步驟2.1 –創建屬性界面
步驟2.2 –為每個配置文件創建一個類 步驟2.3 –創建一個包含所有數據的抽象文件
階段3 :使用模式
步驟3.1 –使用模式的示例
彈簧輪廓圖–階段1:基礎準備
該階段將建立使用Spring Profile和配置文件的初始基礎設施。
步驟1.1 –創建一個包含所有配置數據的屬性文件
假設您有一個maven風格的項目,請在src / main / resources / properties中為每個環境創建一個文件,例如:
my_company_dev.properties
my_company_test.properties
my_company_production.properties
my_company_dev.properties內容的示例:
jdbc.url = jdbc:mysql:// localhost:3306 / my_project_db
db.username = dev1
db.password = dev1 hibernate.show_sql = true
my_company_production.properties內容的示例:
jdbc.url = jdbc:mysql://10.26.26.26:3306 / my_project_db
db.username = prod1
db.password = fdasjkladsof8aualwnlulw344uwj9l34 hibernate.show_sql = false
步驟1.2 –為每個配置文件創建注釋
在src.main.java.com.mycompany.annotation中為每個Profile創建注釋,例如:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("DEV")
public @interface Dev {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("PRODUCTION")
public @interface Production {
}
為每個配置文件創建一個枚舉:
公共接口MyEnums {
public enum Profile{
DEV,
TEST,
PRODUCTION
}
步驟1.3 –確保在上下文加載期間加載了配置文件
- 定義一個系統變量以指示代碼在哪個環境中運行。
在Tomcat中,轉到$ {tomcat.di} /conf/catalina.properties并插入一行:
profile = DEV(根據您的環境) - 定義一個類別以設置活動配置文件
public class ConfigurableApplicationContextInitializer implementsApplicationContextInitializer<configurableapplicationcontext> {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {String profile = System.getProperty("profile");if (profile==null || profile.equalsIgnoreCase(Profile.DEV.name())){applicationContext.getEnvironment().setActiveProfiles(Profile.DEV.name()); }else if(profile.equalsIgnoreCase(Profile.PRODUCTION.name())){applicationContext.getEnvironment().setActiveProfiles(Profile.PRODUCTION.name()); }else if(profile.equalsIgnoreCase(Profile.TEST.name())){applicationContext.getEnvironment().setActiveProfiles(Profile.TEST.name()); }} }
- 確保在上下文加載期間加載了該類
在項目web.xml中,插入以下內容:<context-param><param-name>contextInitializerClasses</param-name><param-value>com.matomy.conf.ConfigurableApplicationContextInitializer</param-value> </context-param>
階段2:實施配置文件模式
此階段利用我們之前構建的基礎架構并實現配置文件模式。
步驟2.1 –創建屬性界面
為您擁有的配置數據創建一個接口。
在我們的例子中,該接口將提供對四個配置數據項的訪問。 所以看起來像這樣:
public interface SystemStrings {String getJdbcUrl();
String getDBUsername();
String getDBPassword();
Boolean getHibernateShowSQL();
//.....
步驟2.2 –為每個配置文件創建一個類
開發配置文件示例:
@Dev //Notice the dev annotation
@Component("systemStrings")
public class SystemStringsDevImpl extends AbstractSystemStrings implements SystemStrings{public SystemStringsDevImpl() throws IOException {//indication on the relevant properties filesuper("/properties/my_company_dev.properties");}
}
生產資料示例:
@Prouction //Notice the production annotation
@Component("systemStrings")
public class SystemStringsProductionImpl extends AbstractSystemStrings implements SystemStrings{public SystemStringsProductionImpl() throws IOException {//indication on the relevant properties filesuper("/properties/my_company_production.properties");}
}
上面的兩個類是屬性文件和相關環境之間進行綁定的位置。
您可能已經注意到,這些類擴展了一個抽象類。 這項技術很有用,因此我們不需要為每個Profile定義每個getter,從長遠來看,這是無法管理的,實際上,這樣做是沒有意義的。
蜜糖和蜂蜜位于下一步,其中定義了抽象類。
步驟2.3 –創建一個包含所有數據的抽象文件
public abstract class AbstractSystemStrings implements SystemStrings{//Variables as in configuration properties file
private String jdbcUrl;
private String dBUsername;
private String dBPassword;
private boolean hibernateShowSQL;public AbstractSystemStrings(String activePropertiesFile) throws IOException {//option to override project configuration from externalFileloadConfigurationFromExternalFile();//optional..//load relevant propertiesloadProjectConfigurationPerEnvironment(activePropertiesFile); }private void loadProjectConfigurationPerEnvironment(String activePropertiesFile) throws IOException {Resource[] resources = new ClassPathResource[ ] { new ClassPathResource( activePropertiesFile ) };Properties props = null;props = PropertiesLoaderUtils.loadProperties(resources[0]);jdbcUrl = props.getProperty("jdbc.url");dBUsername = props.getProperty("db.username"); dBPassword = props.getProperty("db.password");hibernateShowSQL = new Boolean(props.getProperty("hibernate.show_sql"));
}//here should come the interface getters....
階段3:使用模式
您可能還記得,在前面的步驟中,我們定義了一個配置數據接口。
現在,我們將在一個類中使用該接口 ,該類每個環境需要不同的數據。
請注意,這個例子與Spring博客的例子有著關鍵的區別,因為現在我們不需要為每個概要文件創建一個類 ,因為在這種情況下我們在概要文件中使用相同的方法并且僅改變數據 。
步驟3.1 –使用模式的示例
@Configuration
@EnableTransactionManagement
//DB connection configuration class
//(don't tell me you're still using xml... ;-)
public class PersistenceConfig {@Autowiredprivate SystemStrings systemStrings; //Spring will wire by active profile@Beanpublic LocalContainerEntityManagerFactoryBean entityManagerFactoryNg(){LocalContainerEntityManagerFactoryBean factoryBean= new LocalContainerEntityManagerFactoryBean();factoryBean.setDataSource( dataSource() );factoryBean.setPersistenceUnitName("my_pu"); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(){{// JPA propertiesthis.setDatabase( Database.MYSQL);
this.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");this.setShowSql(systemStrings.getShowSqlMngHibernate());//is set per environemnt.. }}; factoryBean.setJpaVendorAdapter( vendorAdapter );factoryBean.setJpaProperties( additionalProperties() );return factoryBean;}
//...
@Beanpublic ComboPooledDataSource dataSource(){ComboPooledDataSource poolDataSource = new ComboPooledDataSource();try {poolDataSource.setDriverClass( systemStrings.getDriverClassNameMngHibernate() );} catch (PropertyVetoException e) {e.printStackTrace();} //is set per environemnt..poolDataSource.setJdbcUrl(systemStrings.getJdbcUrl());poolDataSource.setUser( systemStrings.getDBUsername() );poolDataSource.setPassword( systemStrings.getDBPassword() );//.. more properties... return poolDataSource;}
}
我將不勝感激和改進。
請享用!
參考: Gal Levinsky博客博客中來自JCG合作伙伴 Gal Levinsky的Spring Profile模式 。
翻譯自: https://www.javacodegeeks.com/2012/08/spring-profile-pattern-example_7.html