SpringAOP给类添加新的功能

引入Spring实战中的知识

在SpringAOP中,我们可以为Bean引入新的方法。代理拦截器调用并委托给实现该方法的其他对象。

当引入接口的方法被调用时,代理会把此调用委托给实现了新接口的某给其他对象。

使用注解方式引入

代码

首先是连接点的接口及其实现类

public interface Person {
        void say();
    }
    public class ChinesePerson implements Person {
        @Override
        public void say() {
            System.out.println("说中文");
        }
    }

创建需要添加的功能,这里个人类扩展一个吃的功能

public interface Food {
        void eat();
    }
    public class ChineseFood implements Food {
        @Override
        public void eat() {
            System.out.println("吃中餐");
        }
    }

编写切面

@Aspect
    public class addFuction {
        @DeclareParents(value = "com.wqh.addfunction.Person+",defaultImpl = ChineseFood.class)
        public static Food food;
    }

注意这里的表达式使用的式@DeclareParents注解;该注解所标注的静态属性指明了要引入的接口。
注解中使用的value属性指定哪种类型的bean要引入该接口,这里Person后后面的“+”号表示所有子类型,而不是该类的本身。defaultImpl,指定了为引入功能提供实现的类。

使用XML配置bean:

<!--启用AspectJ的自动代理-->
    <aop:aspectj-autoproxy/>

    <!--声明bean-->
    <bean class="com.wqh.addfunction.addFuction"/>
    <bean name="chinesePerson" class="com.wqh.addfunction.ChinesePerson"/>

测试

@Test
    public void testAdd(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "classpath:spring/applicationContext.xml");
        Person person = (Person) applicationContext.getBean("chinesePerson");
        person.say();
        //这里可以将chinesePerson bean转换为Food类,所以添加成功
        Food food = (Food) applicationContext.getBean("chinesePerson");
        food.eat();
    }

在XML中引入

首先将上面的addFuction注解全部删除,其他不变;然后在xml中添加相应的配置:

<!--启用AspectJ的自动代理-->
    <aop:aspectj-autoproxy/>
    <!--声明bean-->
    <bean name="chinesePerson" class="com.wqh.addfunction.ChinesePerson"/>

     <aop:config>
         <aop:aspect>
             <aop:declare-parents types-matching="com.wqh.addfunction.Person+"
                                  implement-interface="com.wqh.addfunction.Food"
             default-impl="com.wqh.addfunction.ChineseFood"/>
         </aop:aspect>
     </aop:config>

这里的types-matching与上面的vale作用一样;
default-impl与defaultImpl作用一样,这也可以使用delegate-ref;当然如果使用delegate-ref则是要引用SpringBean;
implement-interface则是要引入的接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值