SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍

一、目标

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

二、

1.minstrel.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xmlns:aop="http://www.springframework.org/schema/aop"
 5   xsi:schemaLocation="http://www.springframework.org/schema/aop 
 6       http://www.springframework.org/schema/aop/spring-aop.xsd
 7         http://www.springframework.org/schema/beans 
 8       http://www.springframework.org/schema/beans/spring-beans.xsd">
 9 
10   <bean id="knight" class="chapter01.sia.knights.BraveKnight">
11     <constructor-arg ref="quest" />
12   </bean>
13 
14   <bean id="quest" class="chapter01.sia.knights.SlayDragonQuest">
15     <constructor-arg value="#{T(System).out}" />
16   </bean>
17 
18   <bean id="minstrel" class="chapter01.sia.knights.Minstrel">
19     <constructor-arg value="#{T(System).out}" />
20   </bean>
21 
22   <aop:config>
23     <aop:aspect ref="minstrel">
24       <aop:pointcut id="embark"
25           expression="execution(* *.embarkOnQuest(..))"/>
26         
27       <aop:before pointcut-ref="embark" 
28           method="singBeforeQuest"/>
29 
30       <aop:after pointcut-ref="embark" 
31           method="singAfterQuest"/>
32     </aop:aspect>
33   </aop:config>
34   
35 </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

 1 package chapter01.sia.knights;
 2 
 3 import java.io.PrintStream;
 4 
 5 public class Minstrel {
 6 
 7   private PrintStream stream;
 8   
 9   public Minstrel(PrintStream stream) {
10     this.stream = stream;
11   }
12 
13   public void singBeforeQuest() {
14     stream.println("Fa la la, the knight is so brave!");
15   }
16 
17   public void singAfterQuest() {
18     stream.println("Tee hee hee, the brave knight " +
19             "did embark on a quest!");
20   }
21 
22 }

 

3.KnightMain

 1 public class KnightMain {
 2 
 3   public static void main(String[] args) throws Exception {
 4     //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:knight.xml");
 5     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:minstrel.xml");
 6     Knight knight = context.getBean(Knight.class);
 7     knight.embarkOnQuest();
 8     context.close();
 9   }
10 
11 }

 

4.运行结果

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

 

posted @ 2016-03-01 12:22  shamgod  阅读(348)  评论(0编辑  收藏  举报
haha