Spring增强

1.前置增强

接口:ISomeService

public interface ISomeService {
    public void doSome();
}

public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("==========log==========");

    }
}
public class SomeService implements ISomeService{
    //核心业务
    public void doSome() {
        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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean>
    <!--02.前置增强(通知)-->
    <bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean>
    <!--03.aop-->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>

        <property name="interceptorNames" value="beforeAdvice"></property>
        <property name="proxyTargetClass" value="true"></property>
    </bean>



</beans>

单测

//前置增强
    @Test
    public void test05(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");
        SomeService service = (SomeService) ctx.getBean("proxyService");
        service.doSome();
    }

2.后置增强

接口:ISomeService 

public interface ISomeService {
    public void doSome();
}

public class MyAfterReturningAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("==========after==========");

    }
}
public class SomeService implements ISomeService {
    //核心业务
    public void doSome() {
        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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean>
    <!--02.增强(通知)-->
    <bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean>
    <!--03.aop-->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>

        <property name="interceptorNames" value="afterReturning"></property>
    </bean>

    
</beans>

单测

//后置增强
    @Test
    public void test06(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
    }

3.环绕增强

接口:ISomeService 

public interface ISomeService {
    public void doSome();
}

public class MyMethodInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before");
        methodInvocation.proceed();
        System.out.println("after");
        return null;
    }
}
public class SomeService implements ISomeService {
    //核心业务
    public void doSome() {
        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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean>
    <!--02.增强(通知)-->
    <bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean>
    <!--03.aop-->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>

        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>



</beans>

单测

   //环绕增强
    @Test
    public void test07(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
    }

4.异常增强

接口:ISomeService   Mystoo

public interface ISomeService {
    public void doSome();
}
public interface Mystoo {
    public void run();
    public void run(String style);
}

public class MystooImpl implements Mystoo {
    public void run() {

    }
    public void run(String style) {

    }
}
public class MyThrowsAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception ex){
        int age=6/0;
        System.out.println("错误");
    }
}
public class SomeService implements ISomeService {
    //核心业务
    public void doSome() {
        System.out.println("拜托别让他一番努力换来是奢求!");
    }

    public void doSecont() {

    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    
    <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean>

    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>
        <!--做怎么样的增强-->
        <property name="interceptorNames" value="throwsAdvice"></property>

    </bean>
    
</beans>

单测

 //异常增强
    @Test
    public void test08(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
    }

 

posted @ 2017-07-31 17:03  <烟花易冷>  阅读(176)  评论(0编辑  收藏  举报