Spring四种增强

前置增强   (org.springframework.aop.BeforeAdvice)   表示在目标方法执行前来实施增强

后置增强   (org.springframework.aop.AfterReturningAdvice)   表示在目标方法执行后来实施增强

环绕增强   (org.aopalliance.intercept.MethodInterceptor)   表示在目标方法执行前后同时实施增强

异常抛出增强   (org.springframework.aop.ThrowsAdvice)   表示在目标方法抛出异常后来实施增强

前置增强

创建ISomeService接口和实现类

package cn.happy.day07ProxyFactory;

/**
 * Created by Administrator on 2018/3/8.
 */
public interface ISomeService {
    public void doSome();
}



package cn.happy.day07ProxyFactory;

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

  

创建实体类并实现MethodBeforeAdvice接口

package cn.happy.day07ProxyFactory;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/3/8.
 */
public class BeforeAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强");
    }
}

配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--目标类型-->
<bean id="service" class="cn.happy.day07ProxyFactory.SomeServiceImpl"></bean>

    <!--增强-->
    <bean id="before" class="cn.happy.day07ProxyFactory.BeforeAdvice"></bean>

    <!--代理工厂Bean-->
    <bean id="Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"/>
        <property name="interceptorNames" value="before"/>
    </bean>
</beans>

  

测试结果:

后置增强

后置增强与前置增一样,只是实体类实现接口是AfterReturnningAdvice

这里不做解释

环绕增强

环绕增强需实现MethodInterceptor

实体类方法

package cn.happy.day09ProxyFactory;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/3/8.
 */
public class MethodAdvice implements MethodInterceptor {


    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕增强");
        methodInvocation.proceed();
        System.out.println("环绕增强--");
        return null;
    }
}

  

配置xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--目标类型-->
<bean id="service" class="cn.happy.day09ProxyFactory.SomeServiceImpl"></bean>

    <!--增强-->
    <bean id="method" class="cn.happy.day09ProxyFactory.MethodAdvice"></bean>

    <!--代理工厂Bean-->
    <bean id="Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"/>
        <property name="interceptorNames" value="method"/>
    </bean>
</beans>

  

结果:

异常抛出增强

异常抛出增强需实现ThrowsAdvice

serviceImpl需要伪造一个异常

package cn.happy.day10ProxyFactory;

/**
 * Created by Administrator on 2018/3/8.
 */
public class SomeServiceImpl implements ISomeService {
    public void doSome() {
        //制造异常
        int i=5/0;
        System.out.println("do Something");
    }
}

  

实体类

package cn.happy.day10ProxyFactory;

import org.springframework.aop.ThrowsAdvice;

/**
 * Created by Administrator on 2018/3/8.
 */
public class MyThrowsAdvice implements ThrowsAdvice {

    public void afterThrowing(Exception ex){
        System.out.println("网络异常,异常出现了!!!!");
    }

}

  

配置xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--目标类型-->
<bean id="service" class="cn.happy.day10ProxyFactory.SomeServiceImpl"></bean>

    <!--增强-->
    <bean id="MyThrows" class="cn.happy.day10ProxyFactory.MyThrowsAdvice"></bean>

    <!--代理工厂Bean-->
    <bean id="Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"/>
        <property name="interceptorNames" value="MyThrows"/>
    </bean>
</beans>

  

测试:

package day10;

import cn.happy.day10ProxyFactory.ISomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2018/3/3.
 */
public class Test20180308 {

    //异常增强
    @Test
    public void Spring(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-day10ProxyFactory.xml");
        ISomeService service=(ISomeService)ctx.getBean("Proxy");
        try{
            service.doSome();
        }catch (Exception e){
            e.printStackTrace();
        }

    }


}

  

结果:

原文地址:https://www.cnblogs.com/xuchangqi1/p/8533559.html