Spring之AOP四种实现方式
来源:互联网 发布:爱av淘宝 编辑:程序博客网 时间:2024/05/21 06:52
在spring中配置一个aop的方式共四种
1)动态代理方式:通过spring提供的代理对象实现aop的配置
2)基于spring整合的aspectJ
3)纯POJO
4)基于annotation方式
package com.linkcyzk.service;public interface UserService {public void addUser();}
package com.linkcyzk.service.impl;import com.linkcyzk.service.UserService;/** * 业务层 * @author Administrator */public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("AddUser....................."+this);}}
方式一:动态代理方式
1.需要导入spring-aop.jar
2.编写目标代码
package com.linkcyzk.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class MyAOPBefore implements MethodBeforeAdvice {/** * @param arg0:目标方法的方法对象 * @param arg1:目标方法的参数列表 * @param arg2:目标对象 */@Overridepublic void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {System.out.println(arg0.getName());System.out.println(arg2);System.out.println("MyAopBefore...............");}}
package com.linkcyzk.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class MyAOPAfter implements AfterReturningAdvice {@Overridepublic void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {// TODO Auto-generated method stubSystem.out.println("后置通知aaaaaaaaaaaaa");}}
package com.linkcyzk.aop;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MyAOPMethodIntercepror implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation arg0) throws Throwable {System.out.println("Before####################");arg0.proceed();System.out.println("After####################");return null;}}
package com.linkcyzk.aop;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;public class MyAOPThrow implements ThrowsAdvice {public void afterThrowing(Method method,Object[] args,Object obj,Throwable t) {System.out.println("出错了。。。。。。。。");}}
package com.linkcyzk.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.linkcyzk.service.UserService;/** * 测试类 * @author Administrator */public class Test {public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService us=(UserService) ac.getBean("userServiceAOP"); us.addUser();}}
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--1、配置目标对象 --><bean id="userService" class="com.linkcyzk.service.impl.UserServiceImpl"></bean><!--2、AOP --><bean id="myAopBefore" class="com.linkcyzk.aop.MyAOPBefore"></bean><!--4、配置后置aop通知 --><bean id="myAopAfter" class="com.linkcyzk.aop.MyAOPAfter"></bean><!--5、配置切面 --><bean id="myAOPMethod" class="com.linkcyzk.aop.MyAOPMethodIntercepror"></bean><!--6、配置异常通知 --><bean id="myAopThrow" class="com.linkcyzk.aop.MyAOPThrow"></bean><!--3、置入 --><bean id="userServiceAOP" class="org.springframework.aop.framework.ProxyFactoryBean"><!--3.1配置目标 --><property name="target"><ref bean="userService"/></property><!--3.2配置目标实现的接口 --><property name="proxyInterfaces"><value>com.linkcyzk.service.UserService</value></property><!--3.3配置aop --><property name="interceptorNames"><list><value>myAopBefore</value><value>myAopAfter</value><value>myAOPMethod</value><value>myAopThrow</value></list></property></bean></beans>
方式二、基于spring整合的aspectJ
package com.linkcyzk.aopaspectj;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class MyAOPAspectj implements MethodBeforeAdvice{@Overridepublic void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {System.out.println("before<<<<<<<<<<<<<<<<<<aspectj");}}
package com.linkcyzk.aopaspectj;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class MyAOPAspectjAfter implements AfterReturningAdvice{@Overridepublic void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {System.out.println("MyAOPAspectjAfter>>>>>>>>>>>>>>>>>>>>>"+this);} }
package com.linkcyzk.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.linkcyzk.service.UserService;public class Test2 {public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext2.xml");UserService us=(UserService) ac.getBean("userService");us.addUser();}}
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--1配置切面 --><bean id="myAop" class="com.linkcyzk.aopaspectj.MyAOPAspectj"></bean><bean id="myAopAfter" class="com.linkcyzk.aopaspectj.MyAOPAspectjAfter"></bean><!--2目标 --><bean id="userService" class="com.linkcyzk.service.impl.UserServiceImpl"></bean><!--3置入 --><aop:config><!-- 命名空间 --><aop:pointcut expression="execution(* com.linkcyzk.service.impl.UserServiceImpl.*(..))"id="mycut" /><!--advice-ref="":关联切面pointcut-ref="":切点 --><aop:advisor advice-ref="myAop" pointcut-ref="mycut"/><aop:advisor advice-ref="myAopAfter" pointcut-ref="mycut"/></aop:config></beans>
方式三、纯POJO
package com.linkcyzk.pojo;public class AOPPojo {public void beforePojo(){System.out.println("pojoBefore+++++++++++++++++++");}public void afterPojo(){System.out.println("-------------------------pojoAfter");}}
package com.linkcyzk.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.linkcyzk.service.UserService;public class Test3 {public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext3.xml");UserService us=(UserService) ac.getBean("userService");us.addUser();}}
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置切面 --> <bean id="myAop2" class="com.linkcyzk.pojo.AOPPojo"></bean> <!-- 目标 --> <bean id="userService" class="com.linkcyzk.service.impl.UserServiceImpl"></bean> <!-- 织入 --> <aop:config> <aop:aspect ref="myAop2"> <aop:pointcut expression="execution(* com.linkcyzk.service.impl.UserServiceImpl.*(..)) " id="mycut"/> <aop:before method="beforePojo" pointcut-ref="mycut"/> </aop:aspect> </aop:config> </beans>
方式四 、基于annotation方式
待续。。。。。。。。。。。
阅读全文
0 0
- Spring之AOP四种实现方式
- Spring实现AOP的四种方式
- Spring实现AOP的四种方式
- Spring实现AOP的四种方式
- spring 实现AOP的四种方式
- spring 实现AOP的四种方式
- Spring AOP四种实现方式
- Spring 实现AOP的四种方式
- spring AOP四种实现方式
- Spring AOP 常用的四种实现方式 - Spring之旅
- Spring之AOP实现方式
- Spring AOP 常用的四种实现方式
- Spring AOP 常用的四种实现方式
- Spring实现AOP常用的四种方式
- Spring AOP 常用的四种实现方式
- Spring AOP 常用的四种实现方式
- spring AOP面向切面编程的四种实现方式
- Spring AOP 常用的四种实现方式
- 干货!9种高性能可用高并发的技术架构
- python教程
- 归并排序—递归&非递归
- java自学笔记5:java中的接口
- 自封装MeidaPlayer+SurfaceView 和 TextureView视频播放控件
- Spring之AOP四种实现方式
- Autofac官方文档(二)【新版本】
- 浅谈iOS和Android后台实时消息推送的原理和区别
- 从0开始学习 GITHUB 系列之「向GITHUB 提交代码」
- TIOBE编程语言排行榜,Python遥遥领先
- 优秀控件搜集
- 提升代码可读性的 10 个技巧
- jasper报表通过Java生成pdf中文不显示的一种解决方案
- 运算符