<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--
BeanFactoryPostProcessor 接口是对Bean 工厂的后处理操作。
在Spring 的PropertyPlaceholderConfigurer 类是实现BeanFactoryProcessor 接口中非常有用的类。它用于Spring 从外部属性文件中载入属性,并使用这些属性值替换Spring 配置文件中的占位符变量(${varible})。
Spring 的ApplicationContext 容器可以非常方便的使用PropertyPlaceholderConfigurer,只需通过简单的配置即可使用。
示例:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties" />
</bean>
如果需要使用多个配置文件可以使用PropertyPlaceholderConfigurer 的locations属性。
经过查看源码,发现可以使用locations属性定义多个配置文件:
classpath:指的是项目中src下的文件地址
//elearning.properties中的写法如下
#e-Learning database
elearningDatabaseDriverClassName=com.mysql.jdbc.Driver
#elearningDatabaseUrl=jdbc:mysql://localhost:3306/elearningtest?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8
elearningDatabaseUrl=jdbc:mysql://localhost:3306/elearning2?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8
elearningDatabaseUsername=root
elearningDatabasePassword=root
-->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:elearning.properties"/>
</bean>
<!--
spring 中配置的hibernate
把hibernate的连接数据库放到Spring中来,同时读取外部配置文件中的数据
-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${elearningDatabaseDriverClassName}</value>
</property>
<property name="url">
<value>${elearningDatabaseUrl}</value>
</property>
<property name="username">
<value>${elearningDatabaseUsername}</value>
</property>
<property name="password">
<value>${elearningDatabasePassword}</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>
classpath:/com/amaxgs/elearning/dao/hibernate
</value>
</list>
</property>
</bean>
<bean id="organizationChar" class="com.amaxgs.elearning.bo.OrganizationChar">
<property name="externalHRDAO" ref="IExternalHRDAO"/>
</bean>
</beans>
=======================web。xml==================
===============================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.3"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_3.xsd">
<display-name>e-Learning</display-name>
<description>eLearning Application V2.0</description>
<!--
启动时引入了各种配置文件
contextConfigLocation 参数定义了要装入的 Spring 配置文件,如下面的 servlet 上下文所示
总结:
多个配置文件可以在web.xml里用空格分隔写入,如:
<CONTEXT-PARAM>
<PARAM-NAME>contextConfigLocation</PARAM-NAME>
<PARAM-VALUE>
applicationContext-database.xml applicationContext.xml
</PARAM-VALUE>
</CONTEXT-PARAM>
多个配置文件里的交叉引用可以用ref的external或bean解决
例如:
<bean id="userService" class="domain.user.service.impl.UserServiceImpl">
<property name="dbbean">
<ref bean="dbBean"/>
</property>
</bean>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml,
/WEB-INF/classes/schedules.xml,
/WEB-INF/classes/serviceBeans.xml,
/WEB-INF/classes/daoBeans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBorkerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<display-name>FileUploadServlet</display-name>
<servlet-class>com.amaxgs.elearning.servlet.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/material/upload/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
==========servicesBeans。xml===================
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="ITrainingService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="trainingService"></property>
<property name="transactionAttributes">
<props>
<prop key="apply*">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
<prop key="cancel*">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
<prop key="close*">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
<prop key="complete*">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
<prop key="create*">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly,-ApplicationRuntimeException</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly,-ApplicationRuntimeException</prop>
<prop key="publish*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="trainingService" class="com.amaxgs.elearning.service.spring.TrainingService">
<property name="applyDAO" ref="IApplyDAO"/>
<property name="approveTypeDAO" ref="IApproveTypeDAO"/>
<property name="courseDAO" ref="ICourseDAO"/>
<property name="questionnaireDAO" ref="IQuestionnaireDAO"></property>
<property name="trainingDAO" ref="ITrainingDAO"/>
<property name="userDAO" ref="IUserDAO"/>
<property name="userTrainingDAO" ref="IUserTrainingDAO"/>
<property name="mailService" ref="mailService"/>
<property name="materialService" ref="materialService"/>
<property name="userService" ref="userService"/>
<property name="userTrainingService" ref="userTrainingService"/>
</bean>
</beans>
=================daoBeans。xml================
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean name="IAccountHistoryDAO"
class="com.amaxgs.elearning.dao.hibernate.AccountHistoryDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="IApplyDAO"
class="com.amaxgs.elearning.dao.hibernate.ApplyDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="IApproveDAO"
class="com.amaxgs.elearning.dao.hibernate.ApproveDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="IApproveTypeDAO"
class="com.amaxgs.elearning.dao.hibernate.ApproveTypeDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="IExternalHRDAO"
class="com.amaxgs.elearning.dao.jdbc.ExternalHRDAO">
<property name="dbUrl" value="${hRDatabaseUrl}"/>
<property name="dbUsername" value="${hRDatabaseUsername}"/>
<property name="dbPassword" value="${hRDatabasePassword}"/>
</bean>
</beans>
- <property name="locations">
- <list>
- <value>classpath:config/maxid.properties</value>
- <value>classpath:config/jdoserver.properties</value>
- </list>
- </property>
- Spring的框架中为提供了一个BeanFactoryPostProcessor的实体类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
可以将一些动态参数,移出至.properties中,如此的安排可以让XML定义系统相关设定(不常变动),而.properties可以作为客户根据实际自定义一些相关参数。(如不同人数据库的不同密码)