(13)Spring AOP为目标对象引入新接口
来源:互联网 发布:淘宝店铺模板修改 编辑:程序博客网 时间:2024/06/04 19:03
正常用ioc获取一个bean
接口
package shuai.spring.study.service;public interface IHelloService { public void sayHello();}
实现类
package shuai.spring.study.service.impl;import shuai.spring.study.service.IHelloService;public class HelloServiceImpl implements IHelloService { @Override public void sayHello() { System.out.println("============Hello World!"); }}
配置文件
<?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.xsd"> <bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/> </beans>
测试类
package shuai.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import shuai.spring.study.service.IHelloService;public class HelloTest { @Test public void testHelloWorld() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml"); IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class); iHelloService.sayHello(); }}
测试结果:
============Hello World!
我们可以利用aop让指定的类实现其它的接口,就比如这个shuai.spring.study.service.impl.HelloServiceImpl类,让它实现INiHao接口
定义接口
package shuai.spring.study.service;public interface INiHao { public void sayNiHao(String param);}
实现类
package shuai.spring.study.service.impl;import shuai.spring.study.service.INiHao;public class NiHaoImpl implements INiHao { @Override public void sayNiHao(String param) { System.out.println("你好:" + param); }}修改配置文件
<?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.xsd"> <bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/> <bean id="iNiHao" class="shuai.spring.study.service.impl.NiHaoImpl"/> <!-- <bean id="helloAspect" class="shuai.spring.study.aop.HelloAspect"/> --> <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> <aop:config> <!-- <aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/> --> <aop:aspect ref="helloAspect"> <!-- <aop:before method="beforeHello(java.lang.String,java.lang.String)" pointcut="execution(* shuai.spring.study.service..*.*(..)) and args(param0,param1)" arg-names="param0,param1"/> --> <aop:declare-parents types-matching="shuai.spring.study.service.IHelloService+" implement-interface="shuai.spring.study.service.INiHao" delegate-ref="iNiHao"/> </aop:aspect> </aop:config></beans>
注意,有关引入新接口的内容都写在declare-parents里面
types-matching表示要为哪些类配置新的接口:shuai.spring.study.service.IHelloService+表示实现了IHelloService接口的类
implement-interface表示要为这些类配置什么接口
delegate-ref表示implement-interface所代表接口的默认实现,delegate-ref表示引用一个bean,也可以写成default-impl="shuai.spring.study.service.impl.NiHaoImpl"
测试类
package shuai.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import shuai.spring.study.service.INiHao;public class HelloTest { @Test public void testHelloWorld() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml"); INiHao iHelloService = context.getBean("iHelloService", INiHao.class); iHelloService.sayNiHao("世界"); }}
测试结果:
你好:世界
这个结果跟那个默认实现是有关系的。
阅读全文
0 0
- (13)Spring AOP为目标对象引入新接口
- 【Spring实战】—— 11 通过AOP为特定的类引入新的功能
- 【Spring实战】—— 11 通过AOP为特定的类引入新的功能
- spring-AOP通过注解@DeclareParents引入新的方法
- spring aop获取目标对象的方法对象(包括方法上的注解)
- spring aop获取目标对象的方法对象(包括方法上的注解)
- spring aop获取目标对象的方法对象
- spring aop获取目标对象的方法对象(续)
- 通过Spring Aop编程思想,利用注解为某些对象添加新的功能
- Spring AOP 使用注解为API引入新功能
- Spring AOP--引入增强
- Spring--AOP-引入
- spring-AOP引入
- Spring AOP-通知-为目标方法织入多个切面
- SpringAOP:为目标类添加新的功能(拓展接口)
- spring(十):为特定的类引入新的功能
- 多线程中引入Spring自动注入的对象为null
- Spring的AOP(三):切入点Pointcut 、Advisor及引入
- CSS(三十一)
- Finance系列(2)之Banking
- 171209之JS视频笔记2
- 【Java学习笔记】类之间的关系
- Ciclop开源3D扫描仪软件---Horus源码分析之Image_detection.py
- (13)Spring AOP为目标对象引入新接口
- 快速求和
- 嵌入式Linux开发流程
- CSS(三十二)
- 动态规划入门篇
- CSS(三十三)
- SHA算法Java实现
- Qt struct应用
- 深入理解 Java G1 垃圾收集器