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 ...
随机推荐
- 再跟SQL谈一谈--基础篇
1.简介 2.DDL & DML 3.SELECT ①DISTINCT ②WHERE ③AND & OR ④ORDER BY 4.INSERT 5.UPDATE 6.DELETE 1. ...
- ios Object Encoding and Decoding with NSSecureCoding Protocol
Object Encoding and Decoding with NSSecureCoding Protocol February 27, 2014 MISC NSCoding is a fanta ...
- apache2.2 + tomcat6 整合以及集群配置整理
运行环境:apache2.2.X + tomcat6.0.X + window xp 1. 安装Apache,服务启动后在浏览器中输入http://localhost进行测试,如果能看到一个" ...
- 搭建showslow:前端性能跑分及优化工具
综述:showslow是一个开源的工具,集成并通过Yahoo yslow.google page speed.dynaTrace AJAX等工具监测网站各项性能指标,然后通过图表和排名展示出来. 1. ...
- 对比iOS中的四种数据存储
来自于大牛的文章给大家分享下 :http://www.infoq.com/cn/articles/data-storage-in-ios/
- @class的基本使用
2-@class 的基本使用 1, @class的作用 @class 允许简单的引用类,即类的声明.告诉编译器,后面代码中可能会使用到的类名. 好比函数声明一样. 2, #import的作用 与 #i ...
- linux系统防火墙对访问服务器的影响
一.刚部署好的linux服务器默认开启了防火墙,这时假如你在该服务器装一个tomcat并启动,在别的机器访问该tomcat是不成功的.需要关闭服务器防火墙才可以 二.service iptables ...
- 16_MyBatis中期小结
[MyBatis是什么] MyBatis是一个持久层框架,Mybatis是一个不完全的ORM框架,SQL语句需要程序员自己去编写,但是MyBatis也有映射(输入参数映射.输出结果映射). MyBat ...
- Codevs 4600 [NOI2015]程序自动分析
4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 在实现程序自动分析的过程中,常常需 ...
- MongoDB源码分析——mongo主程序入口分析
Edit 源码版本为MongoDB 2.6分支 mongo主程序入口分析 mongo是MongoDB提供的一个执行JavaScript脚本的客户端工具,可以用来和服务端交互,2.6版本的Mongo ...