spring aop 切面 配置实例

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/cake?useUnicode=true&amp;characterEncoding=utf-8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="99860315"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
	</bean>
	
	<!-- 切面(处理事务) -->
	<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="ShopDAO" class="com.xasxt.cake.po.ShopDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	
	<bean id="CakeDAO" class="com.xasxt.cake.po.CakeDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
		<bean id="OrderformDAO" class="com.xasxt.cake.po.OrderformDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="OrderitemDAO" class="com.xasxt.cake.po.OrderitemDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="CustomerDAO" class="com.xasxt.cake.po.CustomerDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="AddressDAO" class="com.xasxt.cake.po.AddressDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<!-- 原始业务类 Target -->
	<bean id="shopBizTarget" class="com.xasxt.cake.biz.impl.ShopBiz">
		<property name="shopDAO" ref="ShopDAO">
		</property>
	</bean>
	
	<bean id="cakeBizTarget" class="com.xasxt.cake.biz.impl.CakeBiz">
		<property name="cakeDAO" ref="CakeDAO"></property>
	</bean>
	
	<!-- 抽象Bean (模板) -->
	<bean id="BizTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
		<property name="transactionManager" ref="tm"></property>	<!-- 谁来代理 -->
		<property name="proxyTargetClass" value="true"></property>	<!-- 代理方式true:cglib, false:动态代理 -->
		<property name="transactionAttributes">
			<props>
				<!-- 事务的传播行为 -->
				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
	<bean id="shopBiz" parent="BizTemplate">
		<property name="target" ref="shopBizTarget"></property>	<!-- 代理谁? -->
	</bean>
	
	<bean id="cakeBiz2" parent="BizTemplate">
		<property name="target" ref="cakeBizTarget"></property>	<!-- 代理谁? -->
	</bean>
	
	<bean id="customerBizTarget" class="com.xasxt.cake.biz.impl.CustomerBiz">
		<property name="customerDAO" ref="CustomerDAO"></property>
	</bean> 
	
	<bean id="customerBiz" parent="BizTemplate">
		<property name="target" ref="customerBizTarget"></property>
	</bean>
	
	<!-- action -->
	<bean name="/shop" class="com.xasxt.cake.web.struts.action.ShopAction">
		<property name="shopBiz" ref="shopBiz"></property>
	</bean>

	<bean name="/cake" class="com.xasxt.cake.web.struts.action.CakeAction">
		<property name="cakeBiz" ref="cakeBiz"></property>
	</bean>
	
	<bean name="/cart" class="com.xasxt.cake.web.struts.action.CartAction">
		<property name="cakeBiz" ref="cakeBiz"></property>
	</bean>
	
	<bean name="/login" class="com.xasxt.cake.web.struts.action.LoginAction">
		<property name="customerBiz" ref="customerBiz"></property>
	</bean>
	
	<!-- 安全切面 -->
	<bean id="safe" class="com.xasxt.cake.aspect.SafeAsepct"/>
	
	<!-- 配置代理 -->
	<bean id = "cakeBiz" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="cakeBiz2"></property>
		<property name="interceptorNames" value="safe"></property>
	</bean>
	
</beans>

你可能感兴趣的:(spring aop)