一、目标

要在BraveKnight调用embarkOnQuest()前后各做一些处理(调用Minstrel的方法)

二、

1.minstrel.xml

 <?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="knight" class="chapter01.sia.knights.BraveKnight">
<constructor-arg ref="quest" />
</bean> <bean id="quest" class="chapter01.sia.knights.SlayDragonQuest">
<constructor-arg value="#{T(System).out}" />
</bean> <bean id="minstrel" class="chapter01.sia.knights.Minstrel">
<constructor-arg value="#{T(System).out}" />
</bean> <aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut id="embark"
expression="execution(* *.embarkOnQuest(..))"/> <aop:before pointcut-ref="embark"
method="singBeforeQuest"/> <aop:after pointcut-ref="embark"
method="singAfterQuest"/>
</aop:aspect>
</aop:config> </beans>

分析:

(1)首先声明xsi:schemaLocation="http://www.springframework.org/schema/aop

(2)把Minstrel.java声明为bean,给spring管理

(3)声明<aop:config><aop:aspect,指定到ref="minstrel"

(4)声明<aop:pointcut,指定在这些方法中aop,expression="execution(* *.embarkOnQuest(..))"

(5)声明<aop:before、<aop:after


2.Minstrel

 package chapter01.sia.knights;

 import java.io.PrintStream;

 public class Minstrel {

   private PrintStream stream;

   public Minstrel(PrintStream stream) {
this.stream = stream;
} public void singBeforeQuest() {
stream.println("Fa la la, the knight is so brave!");
} public void singAfterQuest() {
stream.println("Tee hee hee, the brave knight " +
"did embark on a quest!");
} }

3.KnightMain

 public class KnightMain {

   public static void main(String[] args) throws Exception {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:knight.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:minstrel.xml");
Knight knight = context.getBean(Knight.class);
knight.embarkOnQuest();
context.close();
} }

4.运行结果

 三月 01, 2016 10:49:57 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f3471d: startup date [Tue Mar 01 10:49:57 CST 2016]; root of context hierarchy
三月 01, 2016 10:49:57 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [minstrel.xml]
Fa la la, the knight is so brave!
Embarking on quest to slay the dragon!
Tee hee hee, the brave knight did embark on a quest!
三月 01, 2016 10:49:58 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3f3471d: startup date [Tue Mar 01 10:49:57 CST 2016]; root of context hierarchy

SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第一章-005-Bean的生命周期

    一. 1. As you can see, a bean factory performs several setup steps before a bean is ready touse. Let’ ...

  2. Spring In Action 第4版笔记-第一章-001架构

    1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...

  3. SPRING IN ACTION 第4版笔记-第一章-004-用类来管理DI

    一. 1. package chapter01.sia.knights.config; import org.springframework.context.annotation.Bean; impo ...

  4. SPRING IN ACTION 第4版笔记-第一章-002-DI介绍

    一. 1.knight.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  5. SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())

    1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...

  6. SPRING IN ACTION 第4版笔记-第九章Securing web applications-010-拦截请求

    一. What if you wanted to restrict access to certain roles only on Tuesday? Using the access() method ...

  7. SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)

    一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mo ...

  8. SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)

    一.LDAP server在哪 By default, Spring Security’s LDAP authentication assumes that the LDAP server is li ...

  9. SPRING IN ACTION 第4版笔记-第九章Securing web applications-004-对密码加密passwordEncoder

    一. 1.Focusing on the authentication query, you can see that user passwords are expected to be stored ...

随机推荐

  1. Python之路【第二十四篇】:Python学习路径及练手项目合集

      Python学习路径及练手项目合集 Wayne Shi· 2 个月前 参照:https://zhuanlan.zhihu.com/p/23561159 更多文章欢迎关注专栏:学习编程. 本系列Py ...

  2. C#的类型、变量和值

    大学学了C#,工作也是使用C#,虽然在日常的开发中没什么大的问题,但个人觉得在C#的理解还不是很清晰,所以决定花一定的时间来理一理学过的知识,顺便革新下脑袋里的知识,因为坑爹的学校在教.net的时候, ...

  3. asp.net 开发问题:Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值。

    "Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值." 这个问题在开发需要上传文件的时候可能会遇到,今天遇到这个问题,百度过也有挺多的修改方法. 方法1: 修 ...

  4. 使用Visual Studio发布应用安装包

    安装包制作方式 使用Visual Studio进行应用的打包分发有两种方式: 1.使用Clickonce发布安装包: 2.使用Setup工程发布安装包. 操作步骤 Clickonce发布安装包 1.右 ...

  5. 转载:为什么Linux不需要磁盘碎片整理

    转载自:www.aqee.net 如果你是个Linux用户,你可能听说过不需要去对你的linux文件系统进行磁盘碎片整理.也许你注意到了,在Liunx安装发布包里没有磁盘碎片整理的工具.为什么会这样? ...

  6. asp.net:验证控件中ValidationExpression的写法

    手机号:"\d{11}"传真号:"\d{7,12}" EMAIL: ".{2,15}@.{2,15}\..{2,4}" 邮箱正则表达式:/^ ...

  7. 01_根据Id查询User的数据

    [工程目录] [数据库表中内容 user表] [sqlMapConfig.xml配置文件主要内容] 简述:sqlMapConfig.xml配置文件主要有两个作用: 1.配置和数据连接的相关信息,例如事 ...

  8. LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)

    Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...

  9. Codevs 3729 飞扬的小鸟

    飞扬的小鸟 标签 动态规划 NOIp提高组 2014 难度 提高+/省选- 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小 ...

  10. SGU 解题报告

    Volume 1 Volume 2