Spring_总结_04_高级配置(一)_Profile
一、前言
本文承接上一节: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的更多相关文章
- Spring_总结_04_高级配置(六)_Bean的初始化和销毁
一.前言 本文承接上一节:Spring_总结_04_高级配置(五)_运行时注入值
- Spring_总结_04_高级配置(五)_运行时注入值
一.前言 本文承接上一节:Spring_总结_04_高级配置(四)_bean的作用域 当讨论依赖注入的时候,我们通常所讨论的是将一个bean引用注入到另一个bean的属性或者构造参数中.它通常指的是将 ...
- Spring_总结_04_高级配置(四)_bean的作用域
一.前言 本文承接上一节:Spring_总结_04_高级配置(三)之处理歧义 1.单例bean Spring应用上下文中所有的bean默认都是单例的.也就是说,不管一个bean被注入到其他bean多少 ...
- Spring_总结_04_高级配置(三)_处理歧义
一.前言 本文承接上一节:Spring_总结_04_高级配置(二)之条件注解@Conditional 我们前面装配bean时,在Spring容器中,都是只有一个bean能匹配所需的结果. 如果有多个b ...
- Spring_总结_04_高级配置(二)_条件注解@Conditional
一.前言 本文承接上一节:Spring_总结_04_高级配置(一)之Profile 在上一节,我们了解到 Profile 为不同环境下使用不同的配置提供了支持,那么Profile到底是如何实现的呢?其 ...
- Sonatype Nexus高级配置
Sonatype Nexus的安装配置参见:CentOS系统中安装Nexus并导入已有的构件库.Nexus内置了Jetty容器,${NEXUS_HOME}/bin/jsw目录下包含了各个操作系统的启动 ...
- [转]JEXUS的高级配置
转自:http://www.cnblogs.com/xiaodiejinghong/archive/2013/04/14/3019660.html 前一回合,我们对服务器软件Jexus作了简单的介绍, ...
- Cisco ASA 高级配置
Cisco ASA 高级配置 一.防范IP分片攻击 1.Ip分片的原理: 2.Ip分片的安全问题: 3.防范Ip分片. 这三个问题在之前已经详细介绍过了,在此就不多介绍了.详细介绍请查看上一篇文章:I ...
- vim的一些高级配置
今天有幸看到一篇博文,有一些vim的高级配置 在linux或者unix下面的.vimrc文件中,在其中可以添加如下片段,可以实现解释上面你说的那些高级用法 " Ctrl + K 插入模式下光 ...
随机推荐
- R语言(一)
向量运算 R的强大功能之一就是把整个数据向量作为一个单一对象来处理.一个数据向量仅是数字的排列,一个向量可以通过如下方式构造 weight<-c(,,,) weight [] 结构c(--)用来 ...
- ajax跨域资源共享
一.同域发送数据 略 二.跨域发送数据 1.存在的问题 1.什么是同源策略 同源策略阻止从一个域名上加载的脚本获取或操作另一个域名上的文档属性.也就是说,受到请求的 URL 的域名必须与当前 Web ...
- Tips for Unix/Linux
@1: 在单个命令中创建目录树:不要逐层创建目录,尽量使用mkdir的-p选项: ~$ mkdir -p one/two/three # 假设目录one不存在 创建复杂的目录树: ~$ mkdir - ...
- 【转】Python爬虫_示例
爬虫项目:爬取汽车之家新闻资讯 # requests+Beautifulsoup爬取汽车之家新闻 import requests from bs4 import BeautifulSoup res ...
- 流量分析系统--zookeeper集群部署
安装zookeeper mkdir apps tar -zxvf zookeeper-3.4.5.tar.gz -C apps [root@mini1 zookeeper-3.4.5]# rm -rf ...
- selection createTextRange setSelectionRange
http://www.cnblogs.com/rainman/archive/2011/02/27/1966482.html http://www.zhangxinxu.com/wordpress/2 ...
- sql中in和exists的区别效率问题 转
in 和exists in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果 ...
- Grafana连接Prometheus监控Docker平台
Grafana是一款开源的分析平台. Grafana allows you to query, visualize, alert on and understand your metrics no m ...
- centOS最小化安装后网络连接问题
编辑配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 修改此行重启后即可 ONBOOT="yes" #修改为 ...
- ASP.NET5 MVC6 利用Middleware 创建可访问HttpContext 的业务类工厂。(代替HttpContext.Current)
我们的目标是在后台业务处理类中,能够很容易的取得用户信息或者其它HTTP请求相关的信息. 所以,首先我们需要一个存储这些信息的类: public class RequestData { public ...