一、前言

本文承接上一节:Spring_总结_03_装配Bean(四)之导入与混合配置

这一节,来总结一下profile。

我们在开发软件时,通常会进行跨环境部署。而在跨环境部署时,经常会遇到某些环境的配置并不适用于另一环境,导致应用无法正常运行。

而profile恰好能解决这个问题。

二、概述

Profile 为不同环境下使用不同的配置提供了支持(开发环境下的配置和生成环境下的配置肯定不同,如数据库、加密算法等配置)

三、配置 Profile bean

要使用profile,首先要将所有不同的bean定义整理到一个或者多个profile中,在将应用部署到每个环境时,要确保对应的profile处于激活状态。

1.Java配置

使用@Profile注解来指定bean所属的环境

@Configuration
public class DataSourceConfig { /**
* 1.开发环境使用嵌入式数据源
* @return
*/
@Bean
@Profile("dev")
public DataSource embeddedDataSource(){
return (DataSource) new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.addScript("classpath:test-data.sql")
.build();
} /**
* 2.生产环境使用JNDI数据源
* @return
*/
@Bean
@Profile("prod")
public DataSource jndiDataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("jdbc/myDS");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
return (DataSource) jndiObjectFactoryBean.getObject();
}
}

如果方法上加了@Profile注解,则对应环境激活时,配置bean会被创建。否则,配置bean会被忽略掉

如代码中配置所示,当dev环境被激活时, embeddedDataSource 会被创建。而 jndiDataSource 会被忽视掉。

2.Xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/plugin"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <beans profile="dev">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
</beans> <beans profile="prod">
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/myDatabase"
resource-ref="true"
proxy-interface="javax.sql.DataSource"/>
</beans>
</beans>

这里配置了2个bean,但在实际运行中,只会创建一个bean,也就是相应环境的bean。

四、激活 Profile

1.激活策略

Spring在确定哪个profile 处于激活状态时,需要依赖两个独立的属性:

spring.profiles.active  和 spring.profiles.default

(1)如果设置了spring.profiles.active 属性,则它的值就会用来确定哪个profile是激活的

(2)如果没有设置spring.profiles.active属性,则Spring将会查找spring.profiles.default的值,用以确定激活的环境

(3)如果这两个属性均没有设置,则没有激活的profile,因此只会创建哪些没有定义在profile中的bean

有多种方式来设置这两个属性:

(1)作为Web应用的上下文参数

(2)作为DispatcherServlet的初始化参数

(3)作为JNDI条目

(4)作为环境变量

(5)作为JVM的系统属性

(6)在集成测试类上,使用@ActiveProfiles注解设置

2.web应用的配置

Servlet2.5以下:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!--设置Spirng配置文件所在位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml,classpath:/spring/applicationContext-*.xml</param-value>
</context-param> <!--1.为上下文设置的默认的profile-->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--2.为DispatcherServlet设置默认的profile-->
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</web-app>

Servlet3.0及以上

public class WebInit implements WebApplicationInitializer {

    @Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("spring.profiles.default", "dev");
}
}

开发时,按照上述配置,所有的开发人员从版本控制软件中拉取代码之后,就能使用开发环境的配置运行代码,而不需要任何额外的配置。

当应用程序部署到QA、生成、或者其他环境之中时,可根据情况使用系统属性、环境变量或者JNDI设置spring.profiles.active即可。

3.使用profile进行测试

在集成测试时,通过使用@AcitveProfiles注解,可以指定运行测试时激活哪个profile

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes ={PersistenceTestConfig.class})
@ActiveProfiles("dev")
public class PersistenceTest { }

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=8rf7kovqfyoo

Spring_总结_04_高级配置(一)_Profile的更多相关文章

  1. Spring_总结_04_高级配置(六)_Bean的初始化和销毁

    一.前言 本文承接上一节:Spring_总结_04_高级配置(五)_运行时注入值

  2. Spring_总结_04_高级配置(五)_运行时注入值

    一.前言 本文承接上一节:Spring_总结_04_高级配置(四)_bean的作用域 当讨论依赖注入的时候,我们通常所讨论的是将一个bean引用注入到另一个bean的属性或者构造参数中.它通常指的是将 ...

  3. Spring_总结_04_高级配置(四)_bean的作用域

    一.前言 本文承接上一节:Spring_总结_04_高级配置(三)之处理歧义 1.单例bean Spring应用上下文中所有的bean默认都是单例的.也就是说,不管一个bean被注入到其他bean多少 ...

  4. Spring_总结_04_高级配置(三)_处理歧义

    一.前言 本文承接上一节:Spring_总结_04_高级配置(二)之条件注解@Conditional 我们前面装配bean时,在Spring容器中,都是只有一个bean能匹配所需的结果. 如果有多个b ...

  5. Spring_总结_04_高级配置(二)_条件注解@Conditional

    一.前言 本文承接上一节:Spring_总结_04_高级配置(一)之Profile 在上一节,我们了解到 Profile 为不同环境下使用不同的配置提供了支持,那么Profile到底是如何实现的呢?其 ...

  6. Sonatype Nexus高级配置

    Sonatype Nexus的安装配置参见:CentOS系统中安装Nexus并导入已有的构件库.Nexus内置了Jetty容器,${NEXUS_HOME}/bin/jsw目录下包含了各个操作系统的启动 ...

  7. [转]JEXUS的高级配置

    转自:http://www.cnblogs.com/xiaodiejinghong/archive/2013/04/14/3019660.html 前一回合,我们对服务器软件Jexus作了简单的介绍, ...

  8. Cisco ASA 高级配置

    Cisco ASA 高级配置 一.防范IP分片攻击 1.Ip分片的原理: 2.Ip分片的安全问题: 3.防范Ip分片. 这三个问题在之前已经详细介绍过了,在此就不多介绍了.详细介绍请查看上一篇文章:I ...

  9. vim的一些高级配置

    今天有幸看到一篇博文,有一些vim的高级配置 在linux或者unix下面的.vimrc文件中,在其中可以添加如下片段,可以实现解释上面你说的那些高级用法 " Ctrl + K 插入模式下光 ...

随机推荐

  1. PL/SQL连接ORACLE失败,ORA-12154: TNS: could not resolve the connect identifier specified

    项目需要使用ORACLE,安装了oracle之后,使用PL/SQL连接,先是提示NOT logger  ,后续不知道改了什么提示解析服务器id失败,重新装了之后更狠的直接来了个空白提示 一.安装PLS ...

  2. 软件工作考核项(zcl)——

    注意:这里没有对代码风格做要求,因为要代码走查! 考核项 考核标准 分数等级   需求规格说明书编写 主要用例图缺失 -1   主要软件界面设计图缺失 -1   主要功能清单项目缺失 -1   主要复 ...

  3. windows安装pywin32

    下载旧版 https://sourceforge.net/projects/pywin32/files/pywin32/ 下载新版 https://github.com/mhammond/pywin3 ...

  4. 0607am抽象类&接口&析构方法&tostring&小知识点

    /*class ren{ public static $color;//静态 static function () { ren::$color; self::$color;//self只能写在类里面, ...

  5. offsetLeft与style.left的区别

    参考:http://www.cnblogs.com/woshilee/articles/1951457.html offsetLeft 获取的是相对于父对象的左边距 left 获取或设置相对于 具有定 ...

  6. 使用新浪IP库获取IP详细地址

    使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...

  7. 013_HDFS文件合并上传putmarge功能(类似于hadoop fs -getmerge)

    场景 合并小文件,存放到HDFS上.例如,当需要分析来自许多服务器的Apache日志时,各个日志文件可能比较小,然而Hadoop更合适处理大文件,效率会更高,此时就需要合并分散的文件.如果先将所有文件 ...

  8. 蓝牙固件升级(OTA升级)原理设计

    转:http://blog.csdn.net/yueqian_scut/article/details/50849033 固件空中升级(OTA)与固件二次引导的原理和设计 原创 2016年03月10日 ...

  9. Laravel 调试利器 —— Laravel Debugbar 扩展包安装及使用教程

    1.简介 Laravel Debugbar 在 Laravel 5 中集成了 PHP Debug Bar ,用于显示调试及错误信息以方便开发.该扩展包包含了一个 ServiceProvider 用于注 ...

  10. awk中打印连续多列,或者删除多列的技巧

    问题:比如有一个文件是20列,你只要后面的18列,怎么打印. 方法:把第一列和第二列做空:用print打印 [wangjq@mgmt humandb]$ cat test 1 2 3 4 5 6 7 ...