Spring之AOP学习小结
在学习aop之前先来解释一下aop一些概念 [list] [*]Joinpoint:它定义在哪里加入你的逻辑功能,对于Spring AOP,Jointpoint指的就是Method。 [*]Advice:特定的Jointpoint处运行的代码,对于Spring AOP 来讲,有Before advice、AfterreturningAdvice、ThrowAdvice、AroundAdvice(MethodInteceptor)等。 [*]Pointcut:一组Joinpoint,就是说一个Advice可能在多个地方织入, [*]Aspect:实际是Advice和Pointcut的组合,但是Spring AOP 中的Advisor也是这样一个东西,但是Spring中为什么叫Advisor而不叫做Aspect。 [*]Target:被通知的对象。 [*]Proxy:将通知应用到目标对象后创建的对象 [*]Weaving:将Aspect加入到程序代码的过程,对于Spring AOP,由ProxyFactory或者ProxyFactoryBean负责织入动作。 [/list] spring对AOP的支持有以下4种情况: [list] [*]经典的基于代理的aop(各版本spring) [*]@AspectJ注解驱动的切面(spring2.0后) [*]纯pojo切面(spring2.0后) [*]注入式AspectJ切面(各版本spring) [/list] ITarget.java [code="java"]package com.myspring.aop; public interface ITarget { public void execute() throws TargetException; } [/code] TargetImpl.java [code="java"]package com.myspring.aop; public class TargetImpl implements ITarget { public TargetImpl() { } public void execute() throws TargetException { System.out.println("main..."); } }[/code] 创建通知 Advice .java [code="java"]package com.myspring.aop; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.ThrowsAdvice; public class Advice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice { public Advice() {} public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("传统经典aop的before..."); } public void afterReturning(Object rtn, Method method, Object[] args, Object target) throws Throwable { System.out.println("传统经典aop的afterReturning..."); } public void afterThrowing(Throwable throwable) { System.out.println("传统经典aop的afterThrowing..."); } public void afterThrowing(Method method, Object[] args, Object target, Throwable throwable) { System.out.println("传统经典aop的afterThrowing2..."); } }[/code] TargetException .java [code="java"]package com.myspring.aop; @SuppressWarnings("serial") public class TargetException extends RuntimeException { public TargetException() {} public TargetException(String message) { super(message); } }[/code] 使用自动代理@AspectJ切面时,需要先注册一个切面AspectAdvice.java [code="java"]package com.myspring.aop; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect//声明切面 public class AspectAdvice { @Pointcut("execution(* *.execute(..))")//定义切点,匹配规则为(返回类型 返回类.方法(参数设置) public void todo(){} @Before("todo()") public void before() { System.out.println("自动代理@AspectJ切面before..."); } @AfterReturning("todo()") public void afterReturning() { System.out.println("自动代理@AspectJ切面afterReturning..."); } @AfterThrowing("todo()") public void afterThrowing() { System.out.println("自动代理@AspectJ切面afterThrowing..."); } }[/code] AdviceBean.java [code="java"]package com.myspring.aop; public class AdviceBean { public void before() { System.out.println("纯POJO切面before..."); } public void afterReturning() { System.out.println("纯POJO切面afterReturning..."); } public void afterThrowing() { System.out.println("纯POJO切面afterThrowing..."); } }[/code] applicationContext.xml [code="xml"] [/code] TestSpringAop.java [code="java"]package com.myspring.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpringAop { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //使用经典的aop ITarget target = (ITarget) ctx.getBean("myAop"); //使用自动代理,可以直接调用目标对象 ITarget autotarget = (ITarget) ctx.getBean("target"); target.execute(); } }[/code]
对于我这初学人员还是很有帮助,多谢分享。
赞成!!
:idea:
我也在看看这块,支持一下!
行家看行道,外行看热闹。 老手看代码,新看看全解。
收下了,,
确实是入门级别的文章。。。不过还是得表扬
spring AOP入门贴,其实spring的中文参考手册上写的更清楚
总结的有点少.