1. 程式人生 > >2018.3.10(動態代理 增強)

2018.3.10(動態代理 增強)

getclass static 文件 catch poi tor lap .so span

JDK動態代理:

準備一個接口ISomeService,接口中有一個方法doSome(),和一個這個接口的實現類SomeServiceImpl,並重寫其中的方法

public interface ISomeService
{
    public void doSome();
}


public class SomeServiceImpl implements ISomeService {
    public void doSome() {
        System.out.println("我不是好人");
    }
}

使用JDK動態代理不需要再配置文件中進行配置,所以現在直接進行測試,但是測試的時候不能使用單測,而是使用main方法進行測試

public class Test {
    public static void main(String[] args) {
        //首先創建一個接口的實現類
        final SomeServiceImpl service=new SomeServiceImpl();
        //在調用方法之前想使用動態代理記錄一下日誌,生成動態代理,返回的是接口
        ISomeService proxyInstance =(ISomeService) Proxy.newProxyInstance(service.getClass().getClassLoader(),
                service.getClass().getInterfaces(), new InvocationHandler() {
                    /**
                     *
                     * @param proxy  代理對象
                     * @param method  目標類型的方法
                     * @param args  方法的參數
                     * @return
                     * @throws Throwable
                     */
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //在這裏記錄一下日誌
                        System.out.println("before=====");
                        //調用method 的
                        method.invoke(service,args);//相當於執行目標類型的方法
                        System.out.println("after=======");
                        return null;
                    }
                });

        //調用動態代理中的方法中的方法
        proxyInstance.doSome();
    }
}
CGLIB動態代理

public class Test {
    public static void main(String[] args) {
        final SomeServiceImpl service=new SomeServiceImpl();
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(service.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            /**
             *
             * @param o 代理對象
             * @param method 目標類型的方法
             * @param objects 目標方法的參數
             * @param methodProxy 代理類的方法   是一個新的參數
             * @return
             * @throws Throwable
             */
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("before=====");
                methodProxy.invoke(service,objects);
                System.out.println("after=====");
                return null;
            }
        });
        SomeServiceImpl proxy =(SomeServiceImpl) enhancer.create();
        proxy.doSome();
    }
}



四中增強: 前置增強 後置增強 環繞增強 異常增強

接口和實體類
public interface ISomeService {
    public void doSome();
}

public class SomeServiceImpl implements ISomeService {
    public void doSome() {
        System.out.println("==A==");
    }
}

前置增強
public class BeforeAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=========before");
    }
}

配置:
<bean id="service" class="demo10.SomeServiceImpl"></bean>
    <bean id="beforeAdvice" class="demo10.BeforeAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="beforeAdvice"></property>
    </bean>

測試類:

@Test
    public void t1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextBefore.xml");
        ISomeService proxyService =(ISomeService) context.getBean("proxyService");
        proxyService.doSome();
    }
後置增強:
<bean id="service" class="demo11.SomeServiceImpl"></bean>
    <bean id="afterAdvice" class="demo11.AfterAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="afterAdvice"></property>
    </bean>

環繞增強

直接引用上面的接口和實現類 再創建另一個類 MethodAdvice

public class MethodAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前置增強");
        Object result = methodInvocation.proceed();
        System.out.println("後置增強");
        return result;
    }
}

配置:
 <bean id="service" class="demo12.SomeServiceImpl"></bean>
    <bean id="methodAdvice" class="demo12.MethodAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>
 
異常增強
public class MyThroesAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception ex){
        System.out.println("網絡出現錯誤");
    }
}

配置:
 <bean id="service" class="demo13.SomeServiceImpl"></bean>
    <bean id="throwsAdvice" class="demo13.MyThroesAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="throwsAdvice"></property>
    </bean>

測試類:

@Test
   public void t2(){
       ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextThrows.xml");
       ISomeService proxyService =(ISomeService) context.getBean("proxyService");
       try{
           proxyService.doSome();
       }catch (Exception ex){
           ex.printStackTrace();
       }

advisor

配置類:
<bean id="service" class="demo14.SomeServiceImpl"></bean> <bean id="beforeAdvice" class="demo14.BeforeAdvice"></bean> <!--與其他不一樣的地方--> <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="mappedNames" value="do*"></property> </bean> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="service"></property> <property name="interceptorNames" value="advisor"></property> </bean>

測試類:
@Test
   public void t2(){
       ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextAdvisor.xml");
       ISomeService proxyService =(ISomeService) context.getBean("proxyService");
       proxyService.doSome();
       proxyService.doAny();
   }





2018.3.10(動態代理 增強)