自动代理

本文介绍了一个使用Spring AOP实现自动代理的例子,通过定义接口IDog1及其实现类Dog1,配合自定义的Before和AfterAdvice,展示了如何在不修改原始类的基础上增加前置和后置操作。

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

<span style="font-size:14px;">public interface IDog1 {  
    public  void  dog();  
    public  void  run();  
}  
public class MyAfter implements AfterReturningAdvice {  
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {  
        System.out.println("=====after===");  
    }  
}  
public class Dog1 implements IDog1 {  
    public  void  dog(){  
        System.out.println("===测试顾问==1dog()");  
    }  
  
    public void run() {  
        System.out.println("====测试顾问===2run()");  
    }  
}  
public class MyBefore1 implements MethodBeforeAdvice {  
    public void before(Method method, Object[] objects, Object o) throws Throwable {  
        System.out.println("======before====");  
    }  
}</span>  

[java] view plain copy
<span style="font-size:14px;"><?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:aop="http://www.springframework.org/schema/aop"  
  
       xmlns:context="http://www.springframework.org/schema/context"  
  
       xmlns:p="http://www.springframework.org/schema/p"  
  
       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="dog" class="cn.springGuWenZiDongDaiLi.Dog1"></bean>  
  
    <bean id="before" class="cn.springGuWenZiDongDaiLi.MyBefore1"></bean>  
  
    <bean id="aftet" class="cn.springGuWenZiDongDaiLi.MyAfter"></bean>  
  
      <!--正则 表达式匹配-->  
    <!--<bean id="GuWen" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
        <property name="advice" ref="before"></property>  
        <property name="patterns" value=".*do.*"></property>  
    </bean>  
    <bean id="GuWen2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
        <property name="advice" ref="aftet"></property>  
        <property name="patterns" value=".*do.*"></property>  
    </bean>-->  
  
    <!--自动代理  缺点只能代理 顾问 模式-->  
    <!--<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>-->  
  
  
    <!--自动代理  代理顾问 也可以代理 通知-->  
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
    <property name="beanNames" value="dog"></property>  
    <property name="interceptorNames" value="aftet"></property>  
</bean>  
  
  
  
</beans>  
</span>  

[java] view plain copy
<span style="font-size:14px;">//顾问 自动 代理  
    @Test  
    public  void  aVoid2(){  
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08GuWenZiDongDaiLi.xml");  
        IDog1 dog = (IDog1)ctx.getBean("dog");  
        dog.dog();  
        System.out.println();  
        dog.run();  
    }</span>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值