设计模式——代理

代理模式为另一个对象提供一个替身或占位符以控制对这个对象的访问。

按照代理的创建时期,代理类可以分为两种。

静态代理:由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存在了。

动态代理:在程序运行时,运用反射机制动态创建而成。

代理类图:


Spring中实现AOP


1.   AOP代理对象

注意在实现代理模式的时候要对目标对象编写代理类,目标对象必须实现了某些接口才行。当要对没有实现接口的目标类进行实现代理时可以用Spring的lib包中的cglib包实现:

编写的代理工厂类为

<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属性。关键的是在切面对象中实现注解的标识:

 

@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配置为

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值