SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍
一、目标
要在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介绍的更多相关文章
- 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’ ...
- Spring In Action 第4版笔记-第一章-001架构
1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...
- SPRING IN ACTION 第4版笔记-第一章-004-用类来管理DI
一. 1. package chapter01.sia.knights.config; import org.springframework.context.annotation.Bean; impo ...
- SPRING IN ACTION 第4版笔记-第一章-002-DI介绍
一. 1.knight.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())
1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- SharePoint 页面Pages和SitePages目录创建不成功解决
最近项目中要用到Pages及SitePages目录中的一个 可是目录时,不是发现没有Pages就是没SitePages: 分析后才得知Pages目录需要开户SharePoint Server Publ ...
- jsp页面中frameset的使用方法
frame,是网页开发必须掌握的知识.例如后台架构.局部刷新,页面分割,都是frame的用途表现,尤其是后台页面制作,使用frame会给用户带来非常舒适的使用感受. frame知识点包括(frames ...
- MVC+Jquery+autocomplete(汉字||拼音首字母搜索)
最近项目中用到了autocomplete了,总结一下经验. 我们先来看一下效果:
- hadoop_集群安装_1
这篇文章中主要介绍的是,如何基于VM安装Linux,以及如何在安装好Linux之后,基于操作系统安装VMTools. 在安装之前,应该先规划好 每个node*的IP地址,以及 hostname: no ...
- C#获取当前路径,获取当前路径的上一层路径
C#获取当前路径的方法如下: (1)string path1 = System.Environment.CurrentDirectory; //C:\...\bin\Debug -获取和设置当前工作目 ...
- 关于添加非系统framework后,import导入头文件时没有提示的解决办法
##1.选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths(如图所示: ##2.双 ...
- Core Canvas–Day1
绘制 1.坐标系统: canvas的坐标以左上角为原点,如图 可对canvas的坐标系统进行变换,变换的方式有 平移translate 旋转rotate 缩放scale 创建自定义的变换方式,切变 2 ...
- javascript面向对象程序设计系列(一)---创建对象
javascript是一种基于对象的语言,但它没有类的概念,所以又和实际面向对象的语言有区别,面向对象是javascript中的难点之一.现在就我所理解的总结一下,便于以后复习: 一.创建对象 1.创 ...
- 将表单数据转化为json数据
/** * 将Form表单转成符合后台要求的json格式数据 * @param frm form表单Id * * @return json格式的数据 */function getFormJson(fr ...
- ubuntu 13.10自定义启动顺序
添加PPA sudo add-apt-repository ppa:danielrichter2007/grub-customizer sudo apt-get update sudo apt-get ...