ApplicationContext中profile指定解读
如果项目需要在开发环境,测试环境,演示环境或生产环境上相互的切换,如何配置web.xml和ApplicationContext.xml文件。
例如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>XXX</display-name>
<!-- Define the pushlet servlet <servlet> <servlet-name>pushlet</servlet-name>
<servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class> <load-on-startup>1</load-on-startup>
</servlet> -->
<!-- The pushlet <servlet-mapping> <servlet-name>pushlet</servlet-name>
<url-pattern>/pushlet.srv</url-pattern> </servlet-mapping> -->
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.cmcc.util.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/applicationContext.xml,
classpath*:spring/applicationContext-extend.xml
</param-value>
</context-param>
<!-- Spring Context的默认Profile,每次部署不同的环境需要改成对应的配置 -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>play</param-value>
<!-- <param-value>${profile.active}</param-value>-->
</context-param>
。。。。。
</web-app>
定义好
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>play</param-value>
<!-- <param-value>${profile.active}</param-value>-->
</context-param>
然后在ApplicationContext.xml文件中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-lazy-init="false">
<description>Spring公共配置 </description>
<context:component-scan base-package="com.cmcc.service,com.cmcc.dao,com.cmcc.exception,com.cmcc.util" />
<!-- <util:properties id="dataSourceProps" location="classpath:system.properties"/> -->
<!--启用aop代理 -->
<aop:aspectj-autoproxy/>
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="#{loadProperties['jdbc.driver']}" />
<property name="url" value="#{loadProperties['jdbc.url']}" />
<property name="username" value="#{loadProperties['jdbc.username']}" />
<property name="password" value="#{loadProperties['jdbc.password']}" />
<property name="defaultAutoCommit" value="false" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="#{loadProperties['dbcp.maxActive']}" />
<property name="maxIdle" value="#{loadProperties['dbcp.maxIdle']}" />
<property name="maxWait" value="#{loadProperties['dbcp.maxWait']}" />
<property name="removeAbandoned" value="#{loadProperties['dbcp.removeAbandoned']}" />
<property name="logAbandoned" value="#{loadProperties['dbcp.logAbandoned']}" />
<property name="removeAbandonedTimeout" value="#{loadProperties['dbcp.removeAbandonedTimeout']}" />
</bean>
。。。。。。
<!-- production环境 -->
<beans profile="production">
<util:properties id="loadProperties" location="classpath:system.properties"/>
</beans>
<!-- local development环境 -->
<beans profile="local">
<util:properties id="loadProperties" location="classpath:application.local.properties"/>
</beans>
<!-- test环境 -->
<beans profile="test">
<util:properties id="loadProperties" location="classpath:application.test.properties"/>
</beans>
<!-- show环境 -->
<beans profile="show">
<util:properties id="loadProperties" location="classpath:application.show.properties"/>
</beans>
<!-- play环境 -->
<beans profile="play">
<util:properties id="loadProperties" location="classpath:application.play.properties"/>
</beans>
。。。。。。。
</beans>
即可
阅读更多
- ApplicationContext中profile指定解读
- VS2015 LINK : fatal error LNK1264: 已指定 /GENPROFILE 但没有所需的代码生成;检测失败
- 使用Selenium+firefox抓取网页指定firefox_profile后的问题
- spring boot1.3.0版本及以上版本profile指定参数无法被打入
- Oracle SQL Profile指定执行计划
- 【bash_profile 带参命令】在指定后缀文件中查找指定内容的命令
- maven: maven编译时指定maven库,即指定profile
- maven根据profile读取指定环境的配置文件
- spring boot执行jar包指定active profile
- 第十章:SpringBoot日志——(指定日志文件和日志Profile功能)
- 通过程序将UserProfile中用户导入到指定SharePoint用户组
- 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口
- coe_xfr_sql_profile.sql 从shared pool、awr中提取指定的SQL ID的Outline Data并创建SQL Profile。
- Idea 使用maven+tomcat的时候,编译指定的Profile
- Python学习函数,指定参数和不指定参数的顺序解读
- Maven 使用profile以文件形式指定开发、生产等不同的环境
- C语言版GetPrivateProfileString()函数,从初始化文件中指定的条目取得字串
- Springboot指定配置文件profile启动
- EBS profile解读和GL学习小结
- Maven 使用profile以占位符的形式指定开发、生产等不同的环境