Spring总结 4.AOP之为对象添加新功能
2017-01-22 23:25
411 查看
Spring除了提供增强原有功能的方法外,还提供了为一个对象引入新功能的方法。如下:
现在为该类的实例化对象引入新方法f()。首先先定义一个接口表示要引入该接口的方法:
既然要有引入新功能,除了接口那肯定还要实现类:
接着使用<aop:declare-parents>标签进行配置:
其中,types-matching表示需要引入新功能的目标类,IService+表示了实现了IService接口的类。implement-interface表示新增的接口,default-impl表示其接口默认实现类。
这样,就完成所有配置。对它进行测试:
package cn.powerfully.service; public interface IService { public void doSomething(int num); }
package cn.powerfully.service; public class ServiceImpl implements IService { @Override public void doSomething(int num) { System.out.println("doSomething --" + num); } }
现在为该类的实例化对象引入新方法f()。首先先定义一个接口表示要引入该接口的方法:
package cn.powerfully.service; public interface IExtraService { public void f(); }
既然要有引入新功能,除了接口那肯定还要实现类:
package cn.powerfully.service; public class ExtraServiceImpl implements IExtraService { @Override public void f() { System.out.println("add"); } }
接着使用<aop:declare-parents>标签进行配置:
<?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" 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-4.2.xsd "> <bean id="aspect" class="cn.powerfully.aspect.Aspect" /> <bean id="userService" class="cn.powerfully.service.ServiceImpl" /> <aop:config> <aop:aspect ref="aspect"> <aop:declare-parents types-matching="cn.powerfully.service.IService+" implement-interface="cn.powerfully.service.IExtraService" default-impl="cn.powerfully.service.ExtraServiceImpl" /> </aop:aspect> </aop:config> </beans>
其中,types-matching表示需要引入新功能的目标类,IService+表示了实现了IService接口的类。implement-interface表示新增的接口,default-impl表示其接口默认实现类。
这样,就完成所有配置。对它进行测试:
package cn.powerfully.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.powerfully.service.IExtraService; import cn.powerfully.service.IService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:beans.xml") public class Demo { @Resource private IService service; @Test public void test1() throws Exception { service.doSomething(1); IExtraService s = (IExtraService)service; s.f(); } }
相关文章推荐
- spring添加<aop:aspectj-autoproxy>让注解自为类生成代理对象时出错
- 通过Spring Aop编程思想,利用注解为某些对象添加新的功能
- ######【spring属性注入(Ioc的DI)总结】:注解方式属性注入,属性名任意.=for理解:Aop注入代理对象时,注入被增强类对象时,属性名为proxy(自定义)。
- SpringAOP之学习总结
- springaop术语概述及aop中常见通知的实现(10级学员 庞丽课堂总结)
- Spring 3 AOP总结 (包含注解式AOP定义, poincut及advice的一些定义方式)
- Spring AOP获取Proxy对象的代码流程
- 为struts的action添加到SPRING的AOP日志拦截器
- Spring Aop机制总结
- 总结从hello word到Spring Aop框架_提炼精髓
- 给内置对象或自定义对象添加存取器属性(getter setter)的方法总结
- vb.net学习总结三(DropDownList 中 添加 StringIntObject 对象的原理)
- SpringAOP[4.源码解析总结(2)]
- spring aop pointcut 如何添加多个execution
- Spring总结之AOP
- 为struts的action添加到SPRING的AOP日志拦截器
- Spring学习总结2(AOP-代理模式)
- 为struts的action添加到SPRING的AOP日志拦截器