Spring-AOP切面+Aspectj框架的使用
来源:互联网 发布:免费数据库 mysql p 编辑:程序博客网 时间:2024/04/29 13:14
使用Aspectj框架进行AOP切面开发。
首先需要导入两个Jar包:
1. aspectjrt.jar
2. aspectjweaver-XXX.jar
Aspectj支持的五种注解:
- @Before: 前置通知, 在方法执行之前执行
- @After: 后置通知, 在方法执行之后执行
- @AfterReturning: 返回通知, 在方法返回结果之后执行
- @AfterThrowing: 异常通知, 在方法抛出异常之后
- @Around: 环绕通知, 围绕着方法执行
下面将使用两种方式进行开发。
形式一 : 使用注解形式结合Spring,
切面:AudienceAdvice类
@Aspectpublic class AudienceAdvice { /** * 1> execution() : 指明一个表达式 * 2> * *..UserService.*(..): 表达式说明 * (1) * : 修饰符 --> 例如:public 或public void * (2) *. : 任意包名或子包名 * (3) .UserService : 该切面只对UserService接口生效 * (4) .* : 所有的方法 * (5) (..) :方法可以有任意参数 * * 作用:任意包下的UserServiceImpl的任意方法,触发此AOP切面 * 3> 表达式可以只写一次,当其他注解继续引用此表达式时, * 直接引用写了表达式的方法名即可 * 例如: init() 方法 和 after() 方法的注解形式 * */ @Pointcut("execution(* *..UserService.*(..))") public void init(){}; /** * 之后 */ @After("init()") public void after(){ System.out.println("after"); } /** * 之前 */ @Before("init()") public void before(){ System.out.println("before"); } /** * 环绕 */ @Around("init()") public void around(ProceedingJoinPoint jp){ System.out.println("before"); try { //执行方法 jp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("after"); }}
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" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- service --> <bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl"> </bean> <!-- 必须要注入此切面,即使下面引用了自动注入 --> <bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" /> <!-- aop自动代理,此标签必须引入 --> <aop:aspectj-autoproxy /></beans>
UserServiceImpl类
public class UserServiceImpl implements UserService{ @Override public void save() { }}
Test部分
public class UserServiceTest { @Test public void saveTest(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml"); UserService userDao = applicationContext.getBean("userServiceImpl", UserService.class); userDao.save(); applicationContext.close(); }}
形式二:使用XML配置Aspectj结合Spring
切面:AudienceAdvice
public class AudienceAdvice { public void init(){}; /** * 之后 */ public void after(){ System.out.println("after"); } /** * 之前 */ public void before(){ System.out.println("before"); } /** * 环绕 */ public void around(ProceedingJoinPoint jp){ System.out.println("before"); try { jp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("after"); }}
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" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl"> </bean> <!-- 注入切面 --> <bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" /> <!-- XML配置切面 --> <aop:config> <!-- 切面 --> <aop:aspect ref="advice"> <!-- 切入点 --> <aop:pointcut expression="execution(* *..UserService.*(..))" id="mypoint"/> <!-- method : 指定运行切面中(AudienceAdvice类)的哪个方法 pointcut-ref : 指定切入点,什么时候运行通知 --> <!-- 前置通知 --> <!-- <aop:before method="before" pointcut-ref="mypoint"/> --> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="mypoint"/> </aop:aspect> </aop:config></beans>
UserServiceImpl服务层:
public class UserServiceImpl implements UserService{ public UserServiceImpl() { System.out.println("初始化"); } @Override public void save() { System.out.println("save"); }}
测试部分:
public class UserServiceTest { @Test public void saveTest(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml"); UserService userDao = applicationContext.getBean("userServiceImpl", UserService.class); userDao.save(); applicationContext.close(); }}
1 0
- Spring-AOP切面+Aspectj框架的使用
- Spring AOP使用整理:使用@AspectJ风格的切面声明
- Spring AOP使用整理:使用@AspectJ风格的切面声明
- java spring aop 支持的AspectJ 切面
- Spring AOP切面编程 AspectJ
- spring AOP切面开发 基于aspectJ框架切点开发
- spring AOP切面开发 基于aspectJ框架切点的注解开发
- aspectj 使用spring AOP切面编程的时候报错:ReflectionWorld$ReflectionWorldException NoClassDefFoundError 的处理
- 关于spring切面使用<aop:aspectj-autoproxy/>报错的解决方法
- 十三Spring Aop以及AspectJ框架的使用一
- 十四Spring Aop以及AspectJ框架的使用二
- Spring AOP编程(两种:1,传统的AOP切面编程 2,基于aspectj框架切点传统开发)
- 深入理解面向切面的编程AOP、AspectJ、Spring
- Spring整合aspectj框架实现的aop
- Spring整合aspectj框架实现的aop
- spring aop, aspectj 注解声明切面
- spring-aop---AspectJ注解式切面编程。
- Spring aspectJ切面使用步骤
- vb4
- 【转】那些情况该使用它们spin_lock到spin_lock_irqsave
- C语言基础 函数
- linux系统软件安装
- linux下搭建一个xampp环境进行性能测试
- Spring-AOP切面+Aspectj框架的使用
- C#中数据库中image类型可转为byte[]类型
- 总线设备驱动模型----驱动篇
- 编写工具展示lucene分词内部分析过程
- Android初学习 - Broadcast Receiver的介绍02
- AOP实现原理——动态代理
- Android性能优化
- Android图片压缩(质量压缩和尺寸压缩)
- Spring快速入门