SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法@DeclareParents、<aop:declare-parents>
一、
1.Introduction的作用是给类动态的增加方法
When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called belongs to the proxied bean or to the introduced interface.
二、例子
要为演出者增加encore功能
1.
package concert; public interface Performance {
void performance();
}
2.
package concert; import org.springframework.stereotype.Component; @Component
public class Eminem implements Performance{ @Override
public void performance() {
System.out.println("you can do anything set to your mind");
} }
3.
package concert; public interface Encoreable {
void performEncore();
}
4.
package concert; public class DefaultEncoreable implements Encoreable{ @Override
public void performEncore() {
System.out.println("DefaultEncoreable---> performEncore()");
} }
5.
package concert; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component; @Aspect
@Component
public class EncoreableIntroducer {
@DeclareParents(value = "concert.Performance+", defaultImpl = DefaultEncoreable.class)
public static Encoreable encoreable;
}
解析:
@DeclareParents的作用是把"Encoreable"介绍给"Performance",它由三部分组成
(1)value:要增加新功能的bean的接口类型
(2)defaultImpl:要增加的功能接口的实现
(3)增加的功能接口的静态变量
6.配置文件
package concert; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class Config { }
7.测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import concert.Config;
import concert.Encoreable; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Config.class)
public class IntroductionTest {
@Autowired
ApplicationContext context; @Test
public void eddieShouldBeAContestant() {
Encoreable eminem = (Encoreable) context.getBean("eminem");
eminem.performEncore();
// Performance eminem = (Performance) context.getBean("eminem");
// eminem.performance();
}
}
另外:xml的配置方法:
<aop:aspect>
<aop:declare-parents types-matching="concert.Performance+" implement-interface="concert.Encoreable" default-impl="concert.DefaultEncoreable" />
</aop:aspect>
As its name implies, <aop:declare-parents> declares that the beans it advises will have new parents in its object hierarchy. Specifically, in this case you’re saying that the beans whose type matches the Performance interface (per the types-matching attribute) should have Encoreable in their parentage (per the implement-interface attribute). The final matter to settle is where the implementation of the Encoreable ’s methods will come from.
还有另一个指定方法用delegate-ref属性
<aop:aspect>
<aop:declare-parents types-matching="concert.Performance+" implement-interface="concert.Encoreable" delegate-ref="encoreableDelegate" />
</aop:aspect>
The delegate-ref attribute refers to a Spring bean as the introduction delegate.This assumes that a bean with an ID of encoreableDelegate exists in the Spring context:
<bean id="encoreableDelegate" class="concert.DefaultEncoreable" />
The difference between directly identifying the delegate using default-impl and indirectly using delegate-ref is that the latter will be a Spring bean that itself may be injected, advised, or otherwise configured through Spring.
<?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="eddie"
class="com.springinaction.springidol.Instrumentalist">
<property name="instrument">
<bean class="com.springinaction.springidol.Guitar" />
</property>
</bean> <!--<start id="audience_bean"/>-->
<bean id="audience"
class="com.springinaction.springidol.Audience" />
<!--<end id="audience_bean"/>--> <!--<start id="audience_aspect"/>-->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression=
"execution(* com.springinaction.springidol.Performer.perform(..))"
/>
<aop:before
pointcut-ref="performance"
method="takeSeats()" />
<aop:before
pointcut-ref="performance"
method="turnOffCellPhones" />
<aop:after-returning
pointcut-ref="performance"
method="applaud" />
<aop:after-throwing
pointcut-ref="performance"
method="demandRefund" />
</aop:aspect> <!--<start id="contestant_introduction"/>-->
<aop:aspect>
<aop:declare-parents
types-matching="com.springinaction.springidol.Performer+"
implement-interface="com.springinaction.springidol.Contestant"
default-impl="com.springinaction.springidol.GraciousContestant"
/>
</aop:aspect>
<!--<end id="contestant_introduction"/>--> <!--
<start id="delegate_ref"/>
<aop:declare-parents
types-matching="com.springinaction.springidol.Performer+"
implement-interface="com.springinaction.springidol.Contestant"
delegate-ref="contestantDelegate"
/>
<end id="delegate_ref"/> <start id="contestant_delegate"/>
<bean id="contestantDelegate"
class="com.springinaction.springidol.GraciousContestant" />
<end id="contestant_delegate"/> --> </aop:config>
<!--<end id="audience_aspect" />--> </beans>
SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法@DeclareParents、<aop:declare-parents>的更多相关文章
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-003-Spring对AOP支持情况的介绍
一. 不同的Aop框架在支持aspect何时.如何织入到目标中是不一样的.如AspectJ和Jboss支持在构造函数和field被修改时织入,但spring不支持,spring只支持一般method的 ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-005-定义切面使用@Aspect、@EnableAspectJAutoProxy、<aop:aspectj-autoproxy>
一. 假设有如下情况,有一个演凑者和一批观众,要实现在演凑者的演凑方法前织入观众的"坐下"."关手机方法",在演凑结束后,如果成功,则织入观众"鼓掌& ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut
一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-002-AOP术语解析
一. 1.Advice Advice是切面的要做的操作,它定义了what.when(什么时候要做什么事) aspects have a purpose—a job they’re meant to d ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-012-AOP总结
1.AOP是面向对象编程的有力补充,它可以让你把分散在应用中的公共辅助功能抽取成模块,以灵活配置,减少了重复代码,让类更关注于自身的功能
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect
一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. package ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML
一. 1.配置文件为xml时则切面类不用写aop的anotation package com.springinaction.springidol; public class Magician impl ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2
一. 情景:有个魔术师会读心术,常人一想一事物他就能读到.以魔术师为切面织入常人的内心. 二. 1. // <start id="mindreader_java" /> ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-008-带参数的ADVICE
一. 假设有情形如:cd里有很多轨,当播放音乐时,要统计每个音轨的播放次数,这些统计操作不应放在播放方法里,因为统计不是播放音乐的主要职责,这种情况适合应用AOP. 二. 1. package sou ...
随机推荐
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- 20160506-hibernate入门
HQL和Criteria HQL(Hibernate Query Language) 面向对象的查询语言,与SQL不同,HQL中的对象名是区分大小写的(除了JAVA类和属性其他部分不区分大小写):HQ ...
- sqlserver中的聚合函数
聚合函数:就是按照一定的规则将多行(Row)数据汇总成一行的函数,对数据进行汇总前,还可以按特定的列(coloumn)将数据进行分组(group by)再汇总,然后按照再次给定的条件进行筛选 一:Co ...
- 创建Unity新项目并编译成游戏程序
注:本人所使用的Unity版本为:Unity5.3.5f1,所使用的VS版本为:Visual.Studio.2013.Ultimate 折腾了快一个月了,终于有时间做自己的啦,哈哈: ) 步骤一:启动 ...
- 暑假集训(2)第三弹 ----- 食物链(poj1182)
C - 食物链 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit ...
- sgu 101 domino
题意还算简洁明了,加上有道翻译凑过着读完了题.题意大体上是 给你 n 个多米诺骨牌, 给出每个骨牌两端的数字, 只有数字相同才可以推到, 比如 2-3和3-2.你可以旋转这些多米诺骨牌, 输出一个可以 ...
- 【转】SQL删除重复数据方法
例如: id name value 1 a pp 2 a ...
- C#获取运行程序的进程ID
C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...
- 图片裁切插件jCrop的使用心得(二)
上一篇简单的介绍了一下开发的背景以及一些学习资料,下面开始介绍如何上手. 一.下载jCrop http://deepliquid.com/content/Jcrop_Download.html 直接去 ...
- NOSQL之【WIN7的安装配置】
1.下载安装 大家根据各自系统配置,下载相对应的redis版本.我下载是redis 2.6 将redis.conf 复制到 bin/release/redisbin 里面 redis-server.e ...