1.使用前准备
Spring在修理@Aspect注解表达式时,需要将Spring的asm模块加到类路径中。asm是轻量级的字节码处理框架,因为java的反射机制无法获取入参名,Spring就利用asm处理@AspectJ中所描述的方法入参名。
此外还需要加入aspectj.weaver和aspectj.tools类包。
2.配置使用@AspectJ切面
自动代理的配置
<aop:aspectj-autoproxy proxy-target-class="true"/> 或<aop:aspectj-autoproxy/>
<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
3.通过注解对类的进行设置
@Before
@After
@AfterReturning
@Around
@AfterThrowing
package com.baobaotao.aspectj.advanced; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; @Aspect public class TestAspect { //-------------复合运算---------- // @Before("!target(com.baobaotao.NaiveWaiter) "+ // "&& execution(* serveTo(..)))") // public void notServeInNaiveWaiter() { // System.out.println("--notServeInNaiveWaiter() executed!--"); // } // @After("within(com.baobaotao.*) " // + " && execution(* greetTo(..)))") // public void greeToFun() { // System.out.println("--greeToFun() executed!--"); // } // // @AfterReturning("target(com.baobaotao.Waiter) || "+ // " target(com.baobaotao.Seller)") // public void waiterOrSeller(){ // System.out.println("--waiterOrSeller() executed!--"); // } // //------------引用命名切点----------// // @Before("TestNamePointcut.inPkgGreetTo()") // public void pkgGreetTo(){ // System.out.println("--pkgGreetTo() executed!--"); // } // // @Before("!target(com.baobaotao.NaiveWaiter) && " // +"TestNamePointcut.inPkgGreetTo()") // public void pkgGreetToNotNaiveWaiter(){ // System.out.println("--pkgGreetToNotNaiveWaiter() executed!--"); // } // //------------访问连接点对象----------// // @Around("execution(* greetTo(..)) && target(com.baobaotao.NaiveWaiter)") // public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable{ // System.out.println("------joinPointAccess-------"); // System.out.println("args[0]:"+pjp.getArgs()[0]); // System.out.println("signature:"+pjp.getTarget().getClass()); // pjp.proceed(); // System.out.println("-------joinPointAccess-------"); // } //------------绑定连接点参数----------// // @Before("target(com.baobaotao.NaiveWaiter) && args(name,num,..)") // public void bindJoinPointParams(int num,String name){ // System.out.println("----bindJoinPointParams()----"); // System.out.println("name:"+name); // System.out.println("num:"+num); // System.out.println("----bindJoinPointParams()----"); // } //------------绑定代理对象----------// // @Before("execution(* greetTo(..)) && this(waiter)") // @Before("this(waiter)") // public void bindProxyObj(Waiter waiter){ // System.out.println("----bindProxyObj()----"); // System.out.println(waiter.getClass().getName()); // System.out.println("----bindProxyObj()----"); // } //------------绑定类标注对象----------// // @Before("@within(m)") // public void bindTypeAnnoObject(Monitorable m){ // System.out.println("----bindTypeAnnoObject()----"); // System.out.println(m.getClass().getName()); // System.out.println("----bindTypeAnnoObject()----"); // } //------------绑定抛出的异常----------// // @AfterReturning(value="target(com.baobaotao.SmartSeller)",returning="retVal") // public void bingReturnValue(int retVal){ // System.out.println("----bingReturnValue()----"); // System.out.println("returnValue:"+retVal); // System.out.println("----bingReturnValue()----"); // } // //------------绑定抛出的异常----------// @AfterThrowing(value="target(com.baobaotao.SmartSeller)",throwing="iae") public void bindException(IllegalArgumentException iae){ System.out.println("----bindException()----"); System.out.println("exception:"+iae.getMessage()); System.out.println("----bindException()----"); } }
其中相关的类
public interface Waiter { @NeedTest public void greetTo(String clientName); public void serveTo(String clientName); } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Monitorable { } @Monitorable public class NaiveWaiter implements Waiter { @NeedTest public void greetTo(String clientName) { System.out.println("NaiveWaiter:greet to "+clientName+"..."); } @NeedTest public void serveTo(String clientName){ System.out.println("NaiveWaiter:serving "+clientName+"..."); } public void smile(String clientName,int times){ System.out.println("NaiveWaiter:smile to "+clientName+ times+"times..."); } } public class NaughtyWaiter implements Waiter { public void greetTo(String clientName) { System.out.println("NaughtyWaiter:greet to "+clientName+"..."); } public void serveTo(String clientName){ System.out.println("NaughtyWaiter:serving "+clientName+"..."); } public void joke(String clientName,int times){ System.out.println("NaughtyWaiter:play "+times+" jokes to "+clientName+"..."); } } public interface Seller { int sell(String goods,String clientName); } public class SmartSeller implements Seller { public int sell(String goods,String clientName) { System.out.println("SmartSeller: sell "+goods +" to "+clientName+"..."); return 100; } public void checkBill(int billId){ if(billId == 1) throw new IllegalArgumentException("iae Exception"); else throw new RuntimeException("re Exception"); } } public class TestNamePointcut { @Pointcut("within(com.baobaotao.*)") private void inPackage(){} @Pointcut("execution(* greetTo(..)))") protected void greetTo(){} @Pointcut("inPackage() and greetTo()") public void inPkgGreetTo(){} }
3.测试例子
package com.baobaotao.aspectj.advanced; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baobaotao.SmartSeller; import com.baobaotao.Waiter; public class AdvancedTest { public static void main(String[] args) { String configPath = "com/baobaotao/aspectj/advanced/beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter"); //NaiveWaiter naiveWaiter1 = (NaiveWaiter) ctx.getBean("naiveWaiter"); Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter"); // naiveWaiter.greetTo("John"); // naiveWaiter.serveTo("John"); // naughtyWaiter.greetTo("Tom"); // naughtyWaiter.serveTo("Tom"); //--通过joinPoint接口访问连接点上下文信息 // naiveWaiter.greetTo("John"); //--绑定连接点参数 //((NaiveWaiter)naiveWaiter).smile("John",2); //naiveWaiter1.smile("John",2); //--绑定代理对象 // naiveWaiter.greetTo("John"); //--绑定类注解 // ((NaiveWaiter)naiveWaiter).greetTo("John"); //绑定返回值 // SmartSeller seller = (SmartSeller) ctx.getBean("seller"); // seller.sell("Beer","John"); //绑定异常 SmartSeller seller = (SmartSeller) ctx.getBean("seller"); seller.checkBill(2); //seller.checkBill(1); } }
相关推荐
NULL 博文链接:https://samter.iteye.com/blog/410618
2 一个简单的基于@AspectJ的AOP例子 2.1 创建Performance接口 创建Performance(表演、演出)接口,接口包含一个方法perform(),包名为aop(本章的类均放在aop包中)。 2.2 创建Performance接口的实现类Ballet 创建...
Spring 使用AspectJ 实现 AOP之前置通知小例子,实际跑过,验证可信。
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
NULL 博文链接:https://tuoxinquyu.iteye.com/blog/1465155
spring aop 自定义切面示例 aspectj 需要相应的架包支持
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....
有人问 Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法? 我这里提供了一种解决方法,仅供参考
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...
为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: ...5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量
第7章 基于@AspectJ和Schema的AOP 7.1 Spring对AOP的支持 7.2 JDK 5.0注解知识快速进阶 7.2.1 了解注解 7.2.2 一个简单的注解类 7.2.3 使用注解 7.2.4 访问注解 7.3 着手使用@AspectJ 7.3.1 使用前的准备 7.3.2 一个...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...
第7章 基于@AspectJ和Schema的AOP 7.1 Spring对AOP的支持 7.2 JDK 5.0注解知识快速进阶 7.2.1 了解注解 7.2.2 一个简单的注解类 7.2.3 使用注解 7.2.4 访问注解 7.3 着手使用@AspectJ 7.3.1 使用前的准备 7.3.2 一个...
下面是一个在spring mvc中关于切面如何使用的例子,可以指直观的理解切面到底有什么作用 1、引用 AspectJ jar 包依赖 pom.xml 文件添加依赖 org.aspectj aspectjrt 1.9.2 2、创建两个新的包 ...
第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。 第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等...
第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。 第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等...
:使用spring jdbc的示例 :使用spring web和servlet api 3的示例spring-aop-example :使用spring aop和AspectJ的示例待办事项:Spring Boot,Spring Web MVC 完整的例子complete-example :包含所有这些元素的微型...