Spring, 基于自动代理实现aop
2015-07-07 15:57
671 查看
与自己写代理实现aop(http://blog.csdn.net/u012994584/article/details/46790267)不同的是:
ApplicationContext.xml:
单元测试:
执行结果:
自动代理优点:减少了代码,少写了PointCut & ProxyFactory
ApplicationContext.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.xsd"> <bean id="studentDaoImpl" class="net.wchdai.spring.dao.impl.StudentDAOImpl"/> <bean id="studentProxy" class="net.wchdai.spring.proxy.MyStudentProxy"/> <!-- <bean id="studentDaoPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*Student"></property> </bean> <bean id="studentDaoProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="studentDaoImpl"></property> <property name="interceptorNames" value="studentDaoAdvisor"></property> <property name="proxyInterfaces" value="net.wchdai.spring.dao.StudentDAO"></property> </bean> --> <bean id="studentDaoAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="studentProxy"></property> <property name="pattern" value=".*Student"></property> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean> </beans>
单元测试:
package net.wchdai.spring.test; import net.wchdai.spring.dao.StudentDAO; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import junit.framework.TestCase; public class StudentAOPTest extends TestCase{ public void testAOPProxy(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml"); // StudentDAO sdao=(StudentDAO)ctx.getBean("studentDaoProxyFactory"); StudentDAO sdao=(StudentDAO)ctx.getBean("studentDaoImpl"); sdao.saveStudent(); } }
执行结果:
before StudentDAOImpl save student afterReturning
自动代理优点:减少了代码,少写了PointCut & ProxyFactory
相关文章推荐
- spring mvc from表单上传后台实现方法(适用android/ios和jsp)
- JAVA多态学习1
- springmvc缓存
- java作业整理
- java细节总结
- springMVC注解方式+easyUI+MYSQL配置实例
- 通过ApplicationContextAware加载Spring上下文环境
- Java中堆内存和栈内存详解
- Eclipse常用快捷键
- java 对象与byte[]互转
- 多线程中synchronized作用
- myeclipse代码中出现 The word 'carousel' is not correctly spelled
- 堆排序(Java实现)
- myeclipse building workspace如何禁止及提高myeclipse速度
- java.security.AccessControlException: access denied (java.util.PropertyPermi
- 【Java】编写一个方法,实现基本的字符串压缩功能
- java线程同步锁synchronized的认识
- Spring2.5学习4.1_静态代理实现
- Spring2.5学习4.1_静态代理实现
- Java中的static关键字解析