一、

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>的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-003-Spring对AOP支持情况的介绍

    一. 不同的Aop框架在支持aspect何时.如何织入到目标中是不一样的.如AspectJ和Jboss支持在构造函数和field被修改时织入,但spring不支持,spring只支持一般method的 ...

  2. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-005-定义切面使用@Aspect、@EnableAspectJAutoProxy、<aop:aspectj-autoproxy>

    一. 假设有如下情况,有一个演凑者和一批观众,要实现在演凑者的演凑方法前织入观众的"坐下"."关手机方法",在演凑结束后,如果成功,则织入观众"鼓掌& ...

  3. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut

    一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...

  4. 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 ...

  5. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-012-AOP总结

    1.AOP是面向对象编程的有力补充,它可以让你把分散在应用中的公共辅助功能抽取成模块,以灵活配置,减少了重复代码,让类更关注于自身的功能

  6. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

    一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. package ...

  7. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML

    一. 1.配置文件为xml时则切面类不用写aop的anotation package com.springinaction.springidol; public class Magician impl ...

  8. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2

    一. 情景:有个魔术师会读心术,常人一想一事物他就能读到.以魔术师为切面织入常人的内心. 二. 1. // <start id="mindreader_java" /> ...

  9. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-008-带参数的ADVICE

    一. 假设有情形如:cd里有很多轨,当播放音乐时,要统计每个音轨的播放次数,这些统计操作不应放在播放方法里,因为统计不是播放音乐的主要职责,这种情况适合应用AOP. 二. 1. package sou ...

随机推荐

  1. asp搜索两个以上的词的原理

    通常会在许多网站上进行搜索一些内容,要输入两个或两个以上的词,它的原理是这样的: 假设在搜索框search中输入:“asp php” 先得到输入框中的内容:search=request("s ...

  2. Opencv——播放视频(带滚动条)

    #include"highgui.h" #include"cv.h" ; CvCapture* g_capture = NULL; void onTrackba ...

  3. sql 中条件in参数问题

    经常遇到条件为in的模糊查询,sql传参可以在service中直接传递参数,不必使用占位符 select * from ud_order where status in ("+status+ ...

  4. 对象创建型模式------Builder(生成器)

    本文系转载,转载地址http://blog.sina.com.cn/s/blog_59b6af690100zj3l.html,只供自己学习使用. 假设现在有三种产品,是玩具,都是由三部分组成,头,身体 ...

  5. ###STL学习--适配器

    点击查看Evernote原文. #@author: gr #@date: 2014-08-24 #@email: forgerui@gmail.com STL中的适配器. ###stl学习 |--迭代 ...

  6. iOS开发——https+证书

    由于公司项目的需要,经过调研后,已成功. 同事已把此问题写成博客<[加密解密]HTTPS>,我就直接转用了 附带链接如下: http://www.jianshu.com/p/8351917 ...

  7. 在Linux上进行QT UI开发

    在QT Creator UI编辑器上通过拖拽各种控件产生UI界面,然后点击编译/Build按钮,会自动生成对应的ui_xxxx.h的 头文件/header file. 参考: 1.Linux上使用Qt ...

  8. Basic MSI silent install

    Articles and post about silent install for Basic MSI, InstallScript, InstallScript MSI: Silent-mode ...

  9. OpenJudge/Poj 1664 放苹果

    1.链接地址: http://bailian.openjudge.cn/practice/1664 http://poj.org/problem?id=1664 2.题目: 总时间限制: 1000ms ...

  10. js方法的命名不能使用表单元素的名称或ID

    今天在写页面的时候,遇到一个关于js方法的命名问题,先看下代码: 表单元素如下: <select name="isCulture" onchange="isCult ...