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();
    }

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();
    }
复制代码

 

 
 
标签: Spring增强
好文要顶 关注我 收藏该文  
0
0
 
 
 
« 上一篇:SPRING代理模式
» 下一篇:Spring 顾问
posted @ 2017-07-31 17:03 <烟花易冷> 阅读(3) 评论(0编辑 收藏
 
 
 
posted @ 2017-08-20 09:16  抱不住太阳的深海  阅读(138)  评论(0编辑  收藏  举报