灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:1931回复:0

Spring中实现AOP

楼主#
更多 发布于:2012-09-08 09:36

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"
     xsichemaLocation="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中的配置则显得明了。



喜欢0 评分0
游客

返回顶部