Spring对方式生成代理---前置、后置增强

本文详细介绍了在Spring中如何通过beans.xml配置实现JDK和CGLIB代理,进行前置和后置增强。展示了在接口上应用多个增强的方法,并提供了测试类以验证增强效果。

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

一、前置增强

beans.xml(使用JDK代理)

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--            增强类-->
    <bean id="greetingAdvice" class="com.testApi.beforeAdvice.GreetingBeforeAdvice"/>
    <bean id="target2" class="com.testApi.beforeAdvice.SuperWaiter"/>
    <bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
          p:proxyInterfaces="com.testApi.beforeAdvice.Waiter"
          p:interceptorNames="greetingAdvice"
          p:target-ref="target2"/>

</beans>
        <!--          proxyInterfaces指明代理要实现对接口
        target2 是要代理对目标类
        interceptorNames 是需要植入目标对象的Bean列表,必须是实现类MethodInterceptor或者Advisor的类
        optimize 为true时强制实行CGLIB代理
        proxyTargetClass 是否对类进行代理,boolean类型
        -->

beans.xml(使用CGLIB代理)

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--            增强类-->
    <bean id="greetingAdvice" class="com.testApi.beforeAdvice.GreetingBeforeAdvice"/>
    <bean id="target2" class="com.testApi.beforeAdvice.SuperWaiter"/>
    <bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
          p:interceptorNames="greetingAdvice"
          p:target-ref="target2"
          p:proxyTargetClass="true"/>

</beans>
        <!--          proxyInterfaces指明代理要实现对接口
        target2 是要代理对目标类
        interceptorNames 是需要植入目标对象的Bean列表,必须是实现类MethodInterceptor或者Advisor的类
        optimize 为true时强制实行CGLIB代理
        proxyTargetClass 是否对类进行代理,boolean类型
        -->

 

一节接口需要多个增强对写法:

p:interceptorNames="aAdvice,bAdvice,cAdvice"

 

测试类:

 @Test
    public void test3(){
        String path= "beans.xml";
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(path);
        Waiter waiter=(Waiter)context.getBean("waiter");
        waiter.greetingTo("hehe");
        waiter.serveTo("liming");
    }

二、后置增强

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--            增强类-->
    <bean id="greetingAdvice" class="com.testApi.beforeAdvice.GreetingBeforeAdvice"/>
    <bean id="afterAdvice" class="com.testApi.beforeAdvice.GreetingAfterAdvice"/>
    <bean id="target2" class="com.testApi.beforeAdvice.SuperWaiter"/>
    <bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
          p:interceptorNames="greetingAdvice,afterAdvice"
          p:target-ref="target2"
          p:proxyTargetClass="true"/>

</beans>
        <!--          proxyInterfaces指明代理要实现对接口
        target2 是要代理对目标类
        interceptorNames 是需要植入目标对象的Bean列表,必须是实现类MethodInterceptor或者Advisor的类
        optimize 为true时强制实行CGLIB代理
        proxyTargetClass 是否对类进行代理,boolean类型
        -->

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值