当前位置:编程学习 > JAVA >>

Spring中实现AOP

1.   AOP代理对象
 注意在实现代理模式的时候要对目标对象编写代理类,目标对象必须实现了某些接口才行。当要对没有实现接口的目标类进行实现代理时可以用Spring的lib包中的cglib包实现:
 编写的代理工厂类为
 [java]
 <strong>public class CglibProxyFactory implements MethodInterceptor{
     private Object targetObject;
  
     public Object createProxyInstance(Object targetObject) {
        this.targetObject = targetObject;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.targetObject.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
     }
  
     @Override
     public Object intercept(Object arg0, Method arg1, Object[]arg2,
            MethodProxy arg3) throws Throwable {
        PersonServiceBean bean = (PersonServiceBean) this.targetObject;
        Object result = null;
        if (bean.getUser() != null) {
            result = arg3.invoke(targetObject, arg2);
        }
       
        return result;
     }
 }</strong>
 
 
2.   spring利用注解进行实现AOP
 首先要编写好目标对象,也就是bean,然后实现好相应的切面对象;在beans.xml中配置好两者的bean属性。关键的是在切面对象中实现注解的标识:
 
 [java]
 @Aspect
 public class MyInterceptor {
  
     @Pointcut("execution(* com.lcq.service.impl.PersonServiceBean.*(..))")
     private void anyMethod() {
     };// 声明一个切入点
  
     @Before("anyMethod() &&args(name)")
     public void doAccessCheck(String name) {
        System.out.println("name:" + name);
        System.out.println("前置通知");
     }
  
     @AfterReturning(pointcut = "anyMethod()", returning = "result")
     public void doAfterReturning(String result) {
        System.out.println("后置通知 :" + result);
        System.out.println("后置通知");
     }
  
     @After("anyMethod()")
     public void doAfter() {
        System.out.println("最终通知");
     }
  
     @AfterThrowing("anyMethod()")
     public void doAfterThrowing() {
        System.out.println("例外通知");
     }
  
     @Around("anyMethod()")
     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("进入方法");
        Object result = pjp.proceed();
        System.out.println("退出方法");
        return result;
     }
 }
 
 
3.   spring利用xml配置的方式实现AOP,利用xml进行的配置除了xml中的配置不同外,其他的Java类相似
 详细的xml配置为
 [java]
 <?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: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-2.5.xsd
           http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  
     <aop:aspectj-autoproxy />
     <beanid="personService" class="com.lcq.service.impl.PersonServiceBean"/>   
        <beanid="aspetbean" class="com.lcq.service.MyInterceptor2"/>
        <aop:config>
            <aop:aspectid="asp" ref="aspetbean">
               <aop:pointcutid="mycut"
                   expression="execution(*com.lcq.service..*.*(..))" />
               <aop:beforepointcut-ref="mycut" method="doAccessCheck"/>
               <aop:after-returningpointcut-ref="mycut"
                   method="doAfterReturning"/>
               <aop:after-throwingpointcut-ref="mycut"
                   method="doAfterThrowing"/>
               <aop:afterpointcut-ref="mycut" method="doAfter" />
               <aop:aroundpointcut-ref="mycut" method="doAround" />
            </aop:aspect>
        </aop:config>
 </beans>
 
总体感觉使用xml的配置方式比较好点,在Java类中编写一些注解,看上去别扭,在xml中的配置则显得明了。
 
 摘自liuchangqing123

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,