SSM-Spring-11:Spring中使用代理工厂Bean实现aop的四种增强

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

说说那四种增强:前置增强,后置增强,环绕增强,异常增强

那什么是代理工厂bean呢?

  org.springframework.aop.framework.ProxyFactoryBean

  就是这个东西,他可以实现对方法的增强

 

@No.1:前置增强:

  需要前置增强的类SomeServiceImpl

 

package cn.dawn.day11aop01;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
    }
}

 

  前置增强的内容的类,可以说实现前置增强接口的类LoggerBefore

 

package cn.dawn.day11aop01;


import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Dawn on 2018/3/5.
 */
/*前置增强*/
public class LoggerBefore implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("日志记录");
    }
}

 

  配置文件中:

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day11aop01.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="before" class="cn.dawn.day11aop01.LoggerBefore"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="before"></property>
    </bean>

</beans>

 

  单测方法:

 

package cn.dawn.day11aop01;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180305 {
    @Test
    /*aop代理工厂bean前置增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day11aop01.xml");
        SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
        service.doSome();
    }
}

 

 

@No.2:后置增强:

  需要后置增强的类:SomeServiceImpl(此类和前置那个写在不同包下,一会的配置文件也不同,往后都是如此)

 

package cn.dawn.day12aop02;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
    }
}

 

  后置增强的内容的的类,他与前置增强的那个实现的接口不同:LoggerAfter

 

package cn.dawn.day12aop02;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
 * Created by Dawn on 2018/3/5.
 */
/*后置增强*/
public class LoggerAfter implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("===============after==================");
    }
}

 

  配置文件中:

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day12aop02.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="afteradvice" class="cn.dawn.day12aop02.LoggerAfter"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="afteradvice"></property>
    </bean>

</beans>

 

  单测方法:

 

package cn.dawn.day12aop02;

        import cn.dawn.day12aop02.SomeServiceImpl;
        import org.junit.Test;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180305 {
    @Test
    /*aop代理工厂bean后置增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day12aop02.xml");
        SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
        service.doSome();
    }
}

 

 

@No.3:环绕增强:

  需要环绕增强的类:SomeServiceImpl

 

package cn.dawn.day13aop03;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
    }
}

 

  环绕增强内容的:

 

package cn.dawn.day13aop03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by Dawn on 2018/3/8.
 */
/*环绕增强需要实现MethodInterceptor这个接口*/
public class MethodAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前置增强");
        /*它可以使前置增强和后置增强分开,同时实现前置和后置*/
        methodInvocation.proceed();
        System.out.println("后置增强");
        return null;
    }
}

 

  配置文件:

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>

</beans>

 

  单测方法:

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>

</beans>

 

 

@No.4:异常增强:

  异常增强是主要用在他出错的时候,进行记录用的,此处模拟起来简单的就是除0异常和空指针异常

  写一个类,它里面的方法存在异常,运行时异常

  SomeServiceImpl类

 

package cn.dawn.day14aop04;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
        String abc=null;
        System.out.println(abc.toString());;
    }
}

 

  此处模拟的是空指针异常,当然异常种类很多,不一一模拟了

  实现ThrowsAdvice接口的类

 

package cn.dawn.day14aop04;

import org.springframework.aop.ThrowsAdvice;

/**
 * Created by Dawn on 2018/3/8.
 */
public class MyThrowsAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception ex){
        System.out.println("网络中断XXXXX-错误号05289");
    }
}

 

  他这个接口里面没有方法要重写,很是奇怪,那是随便写什么defgabc的都可以吗?不是的,翻他的源码,他在上面的注释里提供了模板,只有按照他模板写的才能读取到,此处我用了他的其中一个模板方法

  配置文件中:

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day14aop04.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="myThrowsAdvice" class="cn.dawn.day14aop04.MyThrowsAdvice"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="myThrowsAdvice"></property>
    </bean>

</beans>

 

  单测方法:

 

package cn.dawn.day14aop04;

import cn.dawn.day14aop04.SomeServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180305 {
    @Test
    /*aop代理工厂bean异常增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day14aop04.xml");
        SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
        try{
            service.doSome();
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}

 

    看到这儿的try-catch了吧,他的作用是让单测能通过单元测试,能变成对勾,能执行到结束,不会因为模拟的异常而中断   

--------------------------End-------------------------

基于代理工厂Bean实现aop的四种增强总结完毕

 

posted @ 2018-03-08 21:37  晨曦Dawn  阅读(550)  评论(0编辑  收藏  举报