Spring Profile模式示例

最近,我们介绍了Spring Profiles的概念。

此概念是针对不同部署环境的轻松配置区分符。

直接的用例(已提出)是对相关类进行注释,以便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 implements
      ApplicationContextInitializer<configurableapplicationcontext> {
    
     @Override
     public 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 file
  super("/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 file
  super("/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 externalFile
  loadConfigurationFromExternalFile();//optional..
                //load relevant properties
  loadProjectConfigurationPerEnvironment(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 {

 @Autowired
 private SystemStrings systemStrings; //Spring will wire by active profile

 @Bean
 public LocalContainerEntityManagerFactoryBean entityManagerFactoryNg(){
  LocalContainerEntityManagerFactoryBean factoryBean
  = new LocalContainerEntityManagerFactoryBean();
  factoryBean.setDataSource( dataSource() );
  factoryBean.setPersistenceUnitName("my_pu");       
  JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(){
   {
    // JPA properties
    this.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;
 }
//...
@Bean
 public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值