Spring分散配置

web.xml
[code]<!-- 监听Log4j -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>netctoss.root</param-value>
</context-param>
<!-- 监听Spring文件 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
[/code]

[code] <!-- 在这里import其他的Spring配置文件,那么在web.xml中只监听本文件就可以了 -->
<import resource="springController.xml" />

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="locations">
<list>
<!--
当database.properties放在src目录 下就像下面那样写
classpath:/database.properties
-->
<value> classpath:/com/huanglq/properties/database.properties</value>
</list>
</property>
</bean>

<!-- destroy-method="close"一用完就释放资源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}">
</property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>[/code]


spring配置文件把hibernate.cfg.xml整合进来(eg):
[code] <bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingLocations">
<value>classpath*:/**/*.hbm.xml</value>
</property>
<property name="mappingJarLocations">
<list>
<value>WEB-INF/lib/xxx-core.jar</value>
<value>WEB-INF/lib/xxx-report.jar</value>
</list>
</property>
</bean> [/code]

Spring的applicationContext.xml的例子:
[code]<?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"
default-lazy-init="true">

<!-- 在这里import其他的Spring配置文件,那么在web.xml中只监听本文件就可以了 -->
<import resource="springController.xml" />
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="locations">
<list>
<!--
当database.properties放在src目录 下就像下面那样写
classpath:/database.properties
-->
<value>
classpath:/com/huanglq/properties/database.properties
</value>
</list>
</property>
</bean>

<!-- destroy-method="close"
BasicDataSource提供了close()方法关闭数据源,所以必须设定destroy-method=”close”属性, 以便Spring容器关闭时,数据源能够正常关闭 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driver}">
</property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>

<!--
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/springtrain"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></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>
<!-- 解决过多的POJO -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/huanglq/model</value>
</list>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--在这里abstract="true"属性比较有用,他把target属性赋予了代理事务的目标类 -->
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="sel*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<bean id="studentDao" class="com.huanglq.dao.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="studentService"
class="com.huanglq.service.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean>

<!-- 在DAO级别的代理事务 -->
<bean id="studentDaoTransactionProxy"
parent="baseTransactionProxy">
<property name="target" ref="studentDao" />
</bean>

<bean id="courseDao" class="com.huanglq.dao.CourseDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="courseService"
class="com.huanglq.service.CourseServiceImpl">
<property name="studentService" ref="studentService" />
<property name="courseDao" ref="courseDao" />
</bean>

<bean id="courseDaoTransactionProxy"
parent="baseTransactionProxy">
<property name="target" ref="courseDao" />
</bean>

<bean id="instructorDao"
class="com.huanglq.dao.InstructorDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="instructorService"
class="com.huanglq.service.InstructorServiceImpl">
<property name="instructorDao" ref="instructorDao" />
</bean>

<bean id="instructorDaoTransactionProxy"
parent="baseTransactionProxy">
<property name="target" ref="courseDao" />
</bean>
</beans>[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值