Spring关于增强(前置,后置,异常,环绕。。。)

本文介绍了Spring框架中的四种增强技术:前置增强、后置增强、异常增强和环绕增强。这些增强技术允许在不修改原有接口和实现的情况下,为方法添加额外的功能。详细讨论了MethodBeforeAdvice接口用于前置增强,MethodInterceptor接口用于环绕增强,以及ThrowsAdvice接口用于异常增强。此外,还提到了通过配置文件对特定方法进行增强的两种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring的增强分类:前置增强,后置增强,异常增强,环绕增强。

增强的意义在于我们不改变接口,实现类,不动方法的前提下在原内容上增加内容。

首先我们看一下前置增强:前置增强必须实现的接口:MethodBeforeAdvice

MethodBeforeAdvice

package cn.happy.spring11;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void add();
    public void add1();
    public void add2();


}

package cn.happy.spring11;

/**
 * Created by linlin on 2017/7/31.
 */
public class IDoSomes implements IDoSome{
    public void add() {
        System.out.println("=========1=======");
    }
    public void add1() {
        System.out.println("=========2=======");
    }public void add2() {
        System.out.println("=========3=======");
    }

}

package cn.happy.spring11;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by linlin on 2017/7/31.
 */
public class MyBeforeAdvice implements MethodBeforeAdvice{
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=========before=======");
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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="Idosome" class="cn.happy.spring11.IDoSomes"></bean>
    <bean id="beforeAdvice" class="cn.happy.spring11.MyBeforeAdvice"></bean>
<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="beforeAdvice"></property>

</bean>

</beans>

  @Test
    public void test01(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext07.xml");
        IDoSome i=(IDoSome)ctx.getBean("someProxy");
        i.add();
        i.add1();
        i.add2();
    }




下面是后置增强:实现的接口是:AfterReturningAdvice
package cn.happy.spring12;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void add();
    public String add1();
    public void add2();


}

package cn.happy.spring12;

/**
 * Created by linlin on 2017/7/31.
 */
public class IDoSomes implements IDoSome {
    public void add() {
        System.out.println("=========1=======");
    }
    public String add1() {
        System.out.println("=========2=======");
        return "呵呵abc";
    }

    public void add2() {
        System.out.println("=========3=======");
    }

}

package cn.happy.spring12;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by linlin on 2017/7/31.
 */
public class MyAfterAdvice implements AfterReturningAdvice{

    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("========after=======");
        if(o!=null){
            System.out.println(o.toString().toUpperCase());
        }
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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="Idosome" class="cn.happy.spring12.IDoSomes"></bean>
    <bean id="afterAdvice" class="cn.happy.spring12.MyAfterAdvice"></bean>
<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="afterAdvice"></property>

</bean>

</beans>


 @Test
    public void test02(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08.xml");
        cn.happy.spring12.IDoSome i=(cn.happy.spring12.IDoSome)ctx.getBean("someProxy");
        i.add();
       String id=i.add1();
        System.out.println(id);
        i.add2();
    }


下面是环绕增强:实现接口是:MethodInterceptor 

package cn.happy.spring13;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void add();
    public String add1();
    public void add2();


}

package cn.happy.spring13;

/**
 * Created by linlin on 2017/7/31.
 */
public class IDoSomes implements IDoSome {
    public void add() {
        System.out.println("=========1=======");
    }
    public String add1() {
        System.out.println("=========2=======");
        return "呵呵abc";
    }

    public void add2() {
        System.out.println("=========3=======");
    }

}

package cn.happy.spring13;

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

/**
 * Created by linlin on 2017/7/31.
 */
public class MethodAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("之前============");
       Object result=methodInvocation.proceed();
       String temp=null;
       if(result!=null){
           temp=(String)result;
           temp=temp.toUpperCase();
       }
        System.out.println("之后================");
        System.out.println("======temp========="+temp);
        return temp;
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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="Idosome" class="cn.happy.spring13.IDoSomes"></bean>
    <bean id="methodAdvice" class="cn.happy.spring13.MethodAdvice"></bean>
<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="methodAdvice"></property>

</bean>

</beans>



  @Test
    public void test03(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext09.xml");
        cn.happy.spring13.IDoSome i=(cn.happy.spring13.IDoSome)ctx.getBean("someProxy");
        i.add();
        String id=i.add1();
        System.out.println(id);
        i.add2();
    }


最后一个是异常增强:实现接口是:ThrowsAdvice

package cn.happy.spring14;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void add() throws UserException;
    public String add1();
    public void add2();


}

package cn.happy.spring14;

/**
 * Created by linlin on 2017/7/31.
 */
public class IDoSomes implements IDoSome {
    public void add() throws UserException {
        System.out.println("=========1=======");
        throw new UserException("总算有错了");
    }
    public String add1() {
        System.out.println("=========2=======");
        return "呵呵abc";
    }

    public void add2() {
        System.out.println("=========3=======");
    }

}

package cn.happy.spring14;

import org.springframework.aop.ThrowsAdvice;

/**
 * Created by linlin on 2017/7/31.
 */
public class MyThrowsAdvice implements ThrowsAdvice{
    public void afterThrowing(UserException ex){
        System.out.println("错误提示:"+ex.getMessage());
    }

}

package cn.happy.spring14;

/**
 * Created by linlin on 2017/7/31.
 */
public class UserException extends Exception{
    public UserException() {
        super();
    }

    public UserException(String message) {
        super(message);
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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="Idosome" class="cn.happy.spring14.IDoSomes"></bean>
    <bean id="throwsAdvice" class="cn.happy.spring14.MyThrowsAdvice"></bean>
<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="throwsAdvice"></property>

</bean>

</beans>

  @Test
    public void test04(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext10.xml");
        cn.happy.spring14.IDoSome i=(cn.happy.spring14.IDoSome)ctx.getBean("someProxy");
        try {
            i.add();
        } catch (UserException e) {
            e.printStackTrace();
        }
    }


还有2种配置文件改变 方法的前置增强

package cn.happy.spring16;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void adTd();
    public void adTd1();
    public void add2();


}

package cn.happy.spring16;

/**
 * Created by linlin on 2017/7/31.
 */
public class IDoSomes implements IDoSome {
    public void adTd() {
        System.out.println("=========1=======");
    }
    public void adTd1() {
        System.out.println("=========2=======");
    }
    public void add2() {
        System.out.println("=========3=======");
    }

}

package cn.happy.spring16;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by linlin on 2017/7/31.
 */
public class MyBeforeAdvice implements MethodBeforeAdvice{
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=========before=======");
    }
}


第一种使用方法名字 name 对特定的方法进行增强:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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="Idosome" class="cn.happy.spring15.IDoSomes"></bean>
    <bean id="beforeAdvice" class="cn.happy.spring15.MyBeforeAdvice"></bean>
    <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="mappedNames" value="add"></property>

    </bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="beforeAdvisor"></property>

</bean>

</beans>


第二种 根据名的 .*T*.的这种方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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="Idosome" class="cn.happy.spring16.IDoSomes"></bean>
    <bean id="beforeAdvice" class="cn.happy.spring16.MyBeforeAdvice"></bean>
    <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="patterns" value=".*T.*"></property>

    </bean>

<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="beforeAdvisor"></property>
<property name="proxyTargetClass" value="true"></property>
</bean>

</beans>


下面是测试类 ,都是一样的,。

    @Test
    public void test06(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext12.xml");
        cn.happy.spring16.IDoSome i=(cn.happy.spring16.IDoSome)ctx.getBean("someProxy");
        i.add2();
      i.adTd();
      i.adTd1();
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值