Spring 2.x 的新式Introduction Advice(引入通知) 配置
来源:互联网 发布:2016奥运会网络转播权 编辑:程序博客网 时间:2024/04/29 05:51
我看得是Spring in action,那里面的内容是Spring 1.1的,已经很过时的内容了, aop配置也是旧式的。在别人的提醒下,我开始看Spring 2.x的aop的新配置方法, 现在将这两天的研究结果写下来。
首先,新的有两种配置办法, 一是用注释配置, 二是用<aop>前缀的标签来配置, 这两种都是基于AspectJ的。以前的是Spring自己的AOP的API.
(一)写aspect
还有就是例子所用到的Customer类
(二)增加引入通知
顾客走了,商店的人应该对顾客说拜拜,我们可以用AfterReturning来实现这个功能,但在这里,我决定尝试用Introduction Advice.
配置文件如下这里有一个关键,Shop要是一个接口,而不能是一个具体类
接下来是测试代码
运行结果:
Customer: I want to buy apple
Shop: OK! apple for you
Customer: This is the money
Shop: Now shop is closed
注释的方式跟这个差不多
相信看官方文档应该可以看明白
首先,新的有两种配置办法, 一是用注释配置, 二是用<aop>前缀的标签来配置, 这两种都是基于AspectJ的。以前的是Spring自己的AOP的API.
(一)写aspect
- public interface Shop {
- public void service(Customer customer, String goods);
- }
- public class ShopImpl implements Shop {
- public void service(Customer customer, String goods) {
- customer.buy(good);
- System.out.println("Shop: OK! " + goods + " for you");
- customer.pay();
- }
- }
- public class Customer {
- public void buy(String goods) {
- System.out.println("Customer: I want to buy " + goods);
- }
- public void pay() {
- System.out.println("Customer: This is the money");
- }
- }
顾客走了,商店的人应该对顾客说拜拜,我们可以用AfterReturning来实现这个功能,但在这里,我决定尝试用Introduction Advice.
- public interface Bye {
- public void sayBye();
- }
- public class ByeImpl implements Bye{
- public void sayBye() {
- System.out.println("Shop: Bye!");
- }
- }
- <bean id="shop" class="apple.spring.naop.ShopImpl" />
- <bean id="customer" class="apple.spring.naop.Customer" />
- <aop:aspectj-autoproxy />
- <aop:config>
- <aop:aspect ref="shop">
- <aop:declare-parents
- types-matching="apple.spring.naop.Shop+"
- implement-interface="apple.spring.naop.Bye"
- default-impl="apple.spring.naop.ByeImpl"/>
- </aop:aspect>
- </aop:config>
接下来是测试代码
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");
- Shop shop = (Shop)ctx.getBean("shop");
- Customer customer = (Customer)ctx.getBean("customer");
- shop.service(customer, "apple");
- Bye bye = (Bye)shop;
- bye.sayBye();
- }
Customer: I want to buy apple
Shop: OK! apple for you
Customer: This is the money
Shop: Now shop is closed
注释的方式跟这个差不多
相信看官方文档应该可以看明白
- Spring 2.x 的新式Introduction Advice(引入通知) 配置
- spring的通知(advice)
- spring中通知Introduction的配置
- 5.5 Spring的通知(Advice)
- Spring 基于Introduction的引入
- Introduction的Advice的实现
- Spring切面通知执行的顺序(Advice Order)
- Spring定义事物通知tx:advice
- 16、spring AOP通知——Advice
- Spring AOP通知实例 – Advice
- Spring Aop源码学习--Advice通知
- Spring AOP : Advice 声明 (通知注解)
- spring aop学习3:通知(Advice注解)
- Spring--AOP--基本配置(切面Aspect,切入点Pointcut,通知Advice)
- Advice 通知
- Advice 通知
- spring tx:advice事务配置
- Spring <tx:advice>事务配置
- 使用regsnap查看注册表和文件改动情况
- 使用电脑时保护眼睛:改变窗口颜色保护我们的眼睛
- [转] 分布式计算开源框架Hadoop入门实践
- 用批处理修复IE浏览器
- Linux下软件安装方法总结 [转]
- Spring 2.x 的新式Introduction Advice(引入通知) 配置
- C# 编码规范和编程好习惯
- 哭了
- 团队开发规范
- Groovy学习之资料篇
- Groovy解惑——closure中的delegate
- Groovy解惑——closure中的owner
- Groovy高效编程——用Gant替代Ant(更新于2008.02.18)
- 基本命令(文件系统管理)- -[转]