在配置文件中配置Bean 时,你必须记住,将部署细节如文件路径,服务器地址,用户名称和密码与Bean配置混在一起是不好的做法。通常Bean的配置由开发人员编写,而部署细节因不同的环境而不同,如果开发环境和测试环境以及预发布环境和线上环境等。
如何解决不同环境不同导致的重复修改Bean配置呢?
Spring 有 一个名称为PropertyPlaceHolderConfigurer的Bean工厂后处理器,用来将部分Bean配置外部化为一个属性文件。你可以在 Bean的配置文件中使用${var}形式的变量,PropertyPlaceHolderConfigurer会从属性文件中加载属性并替换他们。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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:jee="http://www.springframework.org/schema/jee"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
">
<context:component-scan base-package="com.david.*" />
<context:property-placeholder location="config.properties" />
<!-- <bean class="org.springframework.remoting.rmi.RmiServiceExporter" p:service-ref="spitterService"
p:serviceName="SpitterService" p:serviceInterface="com.david.service.SpitterService"
/> <bean id="spitterService" class="com.david.service.impl.SpitterServiceImpl"
/> -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- <bean id="spitterServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"
p:serviceUrl="rmi://localhost/SpitterService" p:serviceInterface="com.david.service.SpitterService"
/> -->
<!-- 定义数据源 -->
<bean id="ams" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.ams.driver}" />
<property name="jdbcUrl" value="${jdbc.ams.url}" />
<property name="user" value="${jdbc.ams.username}" />
<property name="password" value="${jdbc.ams.password}" />
<property name="initialPoolSize" value="${initialSize}" />
<property name="minPoolSize" value="${minPoolSize}" />
<property name="maxPoolSize" value="${maxActive}" />
<property name="acquireIncrement" value="${acquireIncrement}" />
<property name="maxIdleTime" value="${maxIdleTime}" />
</bean>
<!-- 定义jdbc模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ams" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addBook" propagation="REQUIRED" />
<tx:method name="deleteBook" propagation="REQUIRES_NEW" />
<tx:method name="addUser" propagation="NESTED"/>
</tx:attributes>
</tx:advice>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ams" />
</bean>
<aop:config>
<aop:advisor
pointcut="execution(* *..david.service.UserService*.*(..))||execution(* *..david.common.facade.ServiceFacade.*(..))||execution(* *..david.service.BookService.*(..))"
advice-ref="txAdvice" />
</aop:config>
</beans>