Aspectj的AOP操作
来源:互联网 发布:电脑屏幕视频录像软件 编辑:程序博客网 时间:2024/05/21 06:49
导包和约束
基于配置文件的AOP操作
//Book.javapackage cn.cstor;public class Book { public void show(){ System.out.println("come from Book.show()..."); //int i = 1/0; }}
//EnhancedBook.javapackage cn.cstor;import org.aspectj.lang.ProceedingJoinPoint;public class EnhancedBook { public void before() { System.out.println("come from EnhancedBook.before().."); } public void after() { System.out.println("come from EnhancedBook.after().."); } public void afterReturnling() { System.out.println("come from EnhancedBook.afterReturnling().."); } public void throwing() { System.out.println("come from EnhancedBook.throwing().."); } public void around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("come from EnhancedBook.around() start..."); joinPoint.proceed(); System.out.println("come from EnhancedBook.around() end..."); }}
<!-- applicationContext.xml --><?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-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对象 --> <bean id="book" class="cn.cstor.Book"/> <bean id="enhancedBook" class="cn.cstor.EnhancedBook"/> <!-- AOP配置--> <aop:config> <!-- 配置切入点 --> <aop:pointcut expression="execution(* cn.cstor.Book.*(..))" id="pointcut1"/> <!-- 配置切面 --> <aop:aspect ref="enhancedBook"> <aop:before method="before" pointcut-ref="pointcut1"/> <aop:after method="after" pointcut-ref="pointcut1"/> <aop:after-returning method="afterReturnling" pointcut-ref="pointcut1"/> <aop:after-throwing method="throwing" pointcut-ref="pointcut1"/> <aop:around method="around" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config></beans>
//BookTest.javapackage cn.cstor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BookTest { @Test public void f(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); ((Book) ac.getBean("book")).show(); }}
基于注解的AOP操作
<!-- applicationContext.xml --><?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-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="book" class="cn.cstor.Book"/> <bean id="enhancedBook" class="cn.cstor.EnhancedBook"/> <!-- 开启aop操作 --> <aop:aspectj-autoproxy/></beans>
//EnhancedBook.javapackage cn.cstor;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class EnhancedBook { @Before("execution(* cn.cstor.Book.*(..))") public void before() { System.out.println("come from EnhancedBook.before().."); } @After("execution(* cn.cstor.Book.*(..))") public void after() { System.out.println("come from EnhancedBook.after().."); } @AfterReturning("execution(* cn.cstor.Book.*(..))") public void afterReturnling() { System.out.println("come from EnhancedBook.afterReturnling().."); } @AfterThrowing("execution(* cn.cstor.Book.*(..))") public void throwing() { System.out.println("come from EnhancedBook.throwing().."); } @Around("execution(* cn.cstor.Book.*(..))") public void around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("come from EnhancedBook.around() start..."); joinPoint.proceed(); System.out.println("come from EnhancedBook.around() end..."); }}
阅读全文
0 0
- Aspectj的AOP操作
- 基于aspectj的aop操作
- Spring的aspectj的AOP操作
- aspectj实现aop的两种操作
- day3_Spring_02_基于aspectj的注解aop操作
- 基于@AspectJ的AOP
- Spring---AspectJ的AOP
- 基于@AspectJ的AOP
- Spring----AspectJ的AOP
- Spring+AspectJ,操作AOP出错
- Spring的AspectJ的AOP
- Spring的AspectJ的AOP(*****)
- Spring的AspectJ的AOP
- <aop:aspectj-autoproxy />的作用
- <aop:aspectj-autoproxy />的作用
- <aop:aspectj-autoproxy /> 的用法
- <aop:aspectj-autoproxy/> 的作用
- 基于aspectj的注解aop
- 自由软件许可证GPL
- Android布局总结一:GridLayout布局(网格布局)
- java 快速排序 优化 以及扩展到对象 List 集合
- 找出字符串中第一个只出现一次的字符
- java继承
- Aspectj的AOP操作
- The working copy “XXXX” failed to commit files.
- 51nod 1630 B君的竞技场
- Spring boot下 Quartz 详细应用配置
- 我的AI转型之路与AI之我见(非985211的奋斗路程与视角)
- Installation failed with message Failed to finalize session : INSTALL_FAILED_NO_MATCHING_ABIS
- [51NOD]-1242 斐波那契数列的第N项 [矩阵快速幂]
- 前端优化之页面回流与重绘
- leetcode--13. Roman to Integer