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>

即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值