面向切面编程:术语

1、Aspect(切面)
组织多个Advice(增强处理),Advice放在切面中定义
代码段1
这就是一个切面类,包含多个Advice,看看注释
configrelax.moniaspectj.java

package configrelax;
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;

@Aspect  //定义一个切面类
public class moniaspectj {

@Before("execution(* configrelax.*.*(..))")  //作为Before增强处理  Advice
public void moni()
{
System.out.println("模拟before检查"); 
}

@AfterReturning(returning="ret",pointcut="execution(* configrelax.*.*(..))")
//目标方法成功完成后被织入,并将返回值给returning  Advice
//不可改变目标方法的返回值
public void log(Object ret)
{
System.out.println("获得目标的返回值"+ret); 
}

@AfterThrowing(throwing="ex",pointcut="execution(* configrelax.*.*(..))")
//处理程序中未处理的异常,但不完全处理,程序会终止  Advice
public void throwex(Throwable ex)
{
 System.out.println("目标方法中的异常"+ex);
 }
 
@After("execution(* configrelax.*.*(..))") 
//作为After增强处理  Advice,不管目标方法如何结束,都会执行
public void moniafter()
{
System.out.println("模拟资源回收"); 
}

@Around("execution(* configrelax.*.*(..))")  
//近似Before和AfterReturning的总和
//下面的方法中回调目标方法,作为分界线,之前的代码处理目标方法之前,之后的代码处理目标方法之后
//可改变目标方法的返回值
public Object around(ProceedingJoinPoint jp)throws java.lang.Throwable
{
 System.out.println("执行目标方法之前,模拟");
 Object ret=jp.proceed();
 System.out.println("目标方法之后,模拟");
 return ret;
}
}

2、Advice(增强处理)
所谓的Advice就是在你的业务逻辑代码前前后后加入一些额外的处理,在代码段1中包含了“Around”、“Before”、“After”等增强处理,实现的功能、方式、不同点看注释
3、Jointpoint(连接点)
程序在执行过程中要处理的点,比如方法的调用、异常的抛出,每个方法被调用都是连接点,一旦连接点被限定了条件,就变成了下面的切入点
4、Pointcut(切入点)
符合限定条件的连接点
configrelax..(…)
configrelax包下所有类的所有方法都是连接点
execution(* configrelax..(…))
执行configrelax包下所有类的这些连接点变成了切入点
5、引入
允许向现有的类添加新方法或属性
6、目标对象
被AOP框架增强处理的对象
7、AOP代理
就是对目标对象的增强
JDK动态代理,谢谢作者,学到了
8、weaving(织入)
将增强处理添加到目标对象中,并创建一个被增强的对象的过程
代码段2
configrelax.Dog.java

package configrelax;
public interface Dog {
public void delete();
public String add(String name,String pass);
}

代码段3
configrelax.gunDog.java

package configrelax;
import org.springframework.stereotype.Component;
@Component("gundog")
 public class gunDog implements Dog
 {
 @Override
 public void delete() {
  // TODO Auto-generated method stub
  System.out.println("执行删除gundog");
 }
 @Override
 public String add(String name,String pass) {
  // TODO Auto-generated method stub
  System.out.println("执行添加"+name);
  if(pass.length()!=6)
   throw new IllegalArgumentException("密码为6位数字");
  return name;
 }
 }

代码段4
configrelax.laDog.java

package configrelax;
import org.springframework.stereotype.Component;
@Component("ladog")
public class laDog implements Dog {
 @Override
 public void delete() {
  // TODO Auto-generated method stub
  System.out.println("执行删除ladog");
 }
 @Override
 public String add(String name,String pass) {
  // TODO Auto-generated method stub
  System.out.println("执行添加"+name);
  if(pass.length()!=6)
   throw new IllegalArgumentException("密码为6位数字");
     return name; 
 }
}

代码段5
beans.xml


   
        
        
        
        
     

代码段6
configrelax.maintest.java

package configrelax;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class maintest {
 public static void main(String[] args)
 { 
 ApplicationContext cn=new ClassPathXmlApplicationContext("beans.xml");
 Dog gundog=cn.getBean("gundog",Dog.class);
 Dog ladog=cn.getBean("ladog",Dog.class);
 ladog.delete();
 ladog.add("lisi","123456");
 gundog.add("zhangsan","1234");
 }
}
执行目标方法之前,模拟
模拟before检查
执行删除ladog
目标方法之后,模拟
模拟资源回收
获得目标的返回值null
执行目标方法之前,模拟
模拟before检查
执行添加lisi
目标方法之后,模拟
模拟资源回收
获得目标的返回值lisi
执行目标方法之前,模拟
模拟before检查
执行添加zhangsan
模拟资源回收
目标方法中的异常java.lang.IllegalArgumentException: 密码为6位数字
Exception in thread "main" java.lang.IllegalArgumentException: 密码为6位数字
 at configrelax.gunDog.add(gunDog.java:20)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
 at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
 at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
 at configrelax.moniaspectj.around(moniaspectj.java:44)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643)
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:632)
 at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
 at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
 at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
 at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
 at com.sun.proxy.$Proxy14.add(Unknown Source)
 at configrelax.maintest.main(maintest.java:12)

你可能感兴趣的:(spring,AOP)