一、AOP之传统手动代理——不带切入点的切面
packagecom.imooc.aop.demo3; publicinterfaceStudentDao { publicvoidfind(); publicvoidsave(); publicvoidupdate(); publicvoiddelete(); }
packagecom.imooc.aop.demo3; publicclassStudentDaoImplimplementsStudentDao { publicvoidfind() { System.out.println("学生查询..."); } publicvoidsave() { System.out.println("学生保存..."); } publicvoidupdate() { System.out.println("学生修改..."); } publicvoiddelete() { System.out.println("学生删除..."); } }
packagecom.imooc.aop.demo3; importorg.springframework.aop.MethodBeforeAdvice; importjava.lang.reflect.Method; publicclassMyBeforeAdviceimplementsMethodBeforeAdvice { publicvoidbefore(Methodmethod, Object[] args, Objecttarget) throwsThrowable { System.out.println("前置增强======================"); } }
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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"><!--配置目标类--><beanid="studentDao"class="com.imooc.aop.demo3.StudentDaoImpl"/><!--前置通知类型--><beanid="myBeforeAdvice"class="com.imooc.aop.demo3.MyBeforeAdvice"/><!--Spring的AOP产生代理对象--><beanid="studentDaoProxy"class="org.springframework.aop.framework.ProxyFactoryBean"><!--配置目标类--><propertyname="target"ref="studentDao"/><!--实现的接口--><propertyname="proxyInterfaces"value="com.imooc.aop.demo3.StudentDao"/><!--采用拦截的名称--><propertyname="interceptorNames"value="myBeforeAdvice"/><propertyname="optimize"value="true"></property></bean></beans>
packagecom.imooc.aop.demo3; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importjavax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") publicclassSpringDemo3 { // @Resource(name="studentDao")@Resource(name="studentDaoProxy") privateStudentDaostudentDao; @Testpublicvoiddemo1(){ studentDao.find(); studentDao.save(); studentDao.update(); studentDao.delete(); } }
运行结果:前置增强======================学生查询... 前置增强======================学生保存... 前置增强======================学生修改... 前置增强======================学生删除...
二、AOP之传统手动代理——带切入点的切面
packagecom.imooc.aop.demo4; publicclassCustomerDao { publicvoidfind(){ System.out.println("查询客户..."); } publicvoidsave(){ System.out.println("保存客户..."); } publicvoidupdate(){ System.out.println("修改客户..."); } publicvoiddelete(){ System.out.println("删除客户..."); } }
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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"><!--配置目标类============--><beanid="customerDao"class="com.imooc.aop.demo4.CustomerDao"/><!--配置通知==============--><beanid="myAroundAdvice"class="com.imooc.aop.demo4.MyAroundAdvice"/><!--一般的切面是使用通知作为切面的,因为要对目标类的某个方法进行增强就需要配置一个带有切入点的切面--><beanid="myAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!--pattern中配置正则表达式:.任意字符*任意次数--><!--<propertyname="pattern"value=".*save.*"/>--><propertyname="patterns"value=".*save.*,.*delete.*"/><propertyname="advice"ref="myAroundAdvice"/></bean><!--配置产生代理--><beanid="customerDaoProxy"class="org.springframework.aop.framework.ProxyFactoryBean"><propertyname="target"ref="customerDao"/><propertyname="proxyTargetClass"value="true"/><propertyname="interceptorNames"value="myAdvisor"/></bean></beans>
packagecom.imooc.aop.demo4; importorg.aopalliance.intercept.MethodInterceptor; importorg.aopalliance.intercept.MethodInvocation; publicclassMyAroundAdviceimplementsMethodInterceptor { publicObjectinvoke(MethodInvocationinvocation) throwsThrowable { System.out.println("环绕前增强==================="); Objectobj=invocation.proceed(); System.out.println("环绕后增强==================="); returnobj; } }
运行结果:查询客户... 环绕前增强===================保存客户... 环绕后增强===================修改客户... 环绕前增强===================删除客户... 环绕后增强===================