引入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则是要引入的接口