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 插入模式下光 ...
随机推荐
- always on 技术
always on 技术系列:https://blog.csdn.net/dba_huangzj/article/details/54015470 MSSQL 2014 /WIN SERVER 200 ...
- c# HttpClient获取网页源码
#region 获取网页源码 public static string HttpClientGetHtmls(string url) { try { var client = new HttpClie ...
- C++语言的I/o使用方法详解
构造器 语法: fstream( const char *filename, openmode mode ); ifstream( const char *filename, openmode mod ...
- Ubuntu 16.04 php卸载
1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...
- (转)【腾讯 TMQ】 接口测试用例设计
导语 这是我在其他的开源社区看到的一篇分享帖子.这篇文章的目的只是为大家提供一个思路,但是实现成本太高了,因为一个接口设计的接口测试用例很多,一般公司的接口数量几百到上千不等,每一个接口都设计这么多测 ...
- FarBox的使用经历
新年伊始,一个崭新的开始,我的博客也有个新的起点.怎么会有这个想法呢?个人觉得这是程序员那颗不安分的心开始躁动了(其实就是开始作了~~哈哈,开个玩笑). 更佳界面.更流畅的操作.更方便的查看.更炫酷动 ...
- HTTP学习笔记05-首部
首部和方法配合工作共同决定了客户端和服务器能做些什么事情. 首部可以出现在请求和响应报文中,大致来分的话,可以分为那么5种: 通用首部: request和response报文都可以使用的首部. 比如 ...
- 测试 js 方法运行时间
早期测速的时候是这样的,呵呵呵,一开始还挺爽的 function testFunctionTime(fn) { var start = new Date().getTime(); if (fn) fn ...
- Centos系统 上下文切换的检查思路
1.什么是上下文切换(Context Switch)? 上下文切换,有时也称做进程切换或任务切换,是指CPU从一个进程或线程切换到另一个进程或线程. 操作系统可以同时运行多个进程, 然而一颗CPU同时 ...
- MongoDB 使用Limit和Skip完成分页 和游标(二)
//$slice操作符返回文档中指定数组的内部值 //查询出Jim书架中第2~4本书 db.persons.find({name:"jim"},{books:{"$sli ...