spring context配置

<?xml version="1.0" encoding="UTF-8"?>
<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:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
   
	<!--对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.csnt.ecs">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
		
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath:mail.properties</value>
				<value>classpath:memcache.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
	</bean>

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
		<property name="jndiName" value="java:comp/env/jdbc/xxxds"/> </bean> -->

	<!-- 邮件发送 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="protocol" value="${mail.transport.protocol}" />
		<property name="host" value="${mail.smtp.host}" />
		<property name="port" value="${mail.smtp.port}" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
				<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
			</props>
		</property>
		<property name="username" value="${mail.smtp.username}" />
		<property name="password" value="${mail.smtp.password}" />
		<property name="defaultEncoding" value="${mail.smtp.encode}" />
	</bean>

	<!-- 配置事务 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="send*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="edit" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="create*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="insert*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="remove*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="audit*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="sync*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="init*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="count*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 开启注解配置 -->
	<aop:aspectj-autoproxy />

	<!-- 基于接口VS类的代理被创建 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="false" />

	<!-- 配置参与事务的类,控制到Service层 -->
	<aop:config>
		<aop:pointcut id="serviceMethodTransactionManager"
			expression="within(com.csnt.ecs.core.service.impl.BaseService+)" />
		<aop:advisor pointcut-ref="serviceMethodTransactionManager"
			advice-ref="txAdvice" />
	</aop:config>

</beans>

相关推荐