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 ...
随机推荐
- SMTP ERROR: Password command failed: 535 Incorrect authentication data
在处理一个使用PHPMailer来发送电邮,我在本地使用我的163邮箱来做测试发送电邮,能够成功的发送电邮:当上传到正式平台时,出现了,类似这样的错误信息 SMTP ERROR: Password c ...
- java 静态方法
在使用java的时候,你会发现,有些对象,需要new ,有些则不需要时,比如Math类 ); 如果你查看源码就会大致的发现,里面的属性和方法都是静态的: public static double si ...
- lsjORM ----让开发变得更加快捷(二)
lsjORM结构 跟传统三层没有多大区别,这里添加DTO(参数列表)跟PetaPoce(数据库操作),普通的三层我们都喜欢用DBHelper或者SqlHelper来封装sql的辅助方法,PetaPoc ...
- web开发学习之旅---css第一天
一.css全称 Cascade Style Sheet层叠样式表 二.css引入方式 行内样式:<h2 style="color:#0F0">Hello World&l ...
- 更靠谱的js判断浏览器及其版本
所有的前端开发人员都没有办法回避一个问题,那就是浏览器版本判断,当我们无法回避需要进行浏览器版本判断时,前辈们往往会告诉我们,可以判断 UserAgent这个用来标示浏览器的字符串,通过判断这一字符串 ...
- 设置repeater每行多少个的方法
前台代码: <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">< ...
- 20160406javaweb 之JDBC简单案例
前几天写的user注册登录注销案例,没有用到数据库,现在做出改动,使用数据库存储信息: 一.首先我们需要建立一个数据库: 如下图: 创建数据库的代码如下: -- 导出 database02 的数据库结 ...
- 那天有个小孩跟我说LINQ(五)转载
2 LINQ TO SQL(代码下载) 我们以一个简单的销售的业务数据库为例子 表结构很简单:Users(购买者(用户)表),Products(产品信息表),Sales(销 ...
- cognos 10.2.2 Framework manager使用”数据源”新建查询主题
又做了一个简单的报表,就是在Framework Manager中写个sum()的sql出个报表,可以使用使用"数据源"新建查询主题 配置查询主题后修改SQL,注意全部都是大写,要和 ...
- swift入门-day02
1.函数 2.闭包 3.构造函数基础 4.重载构造函数 5.KVC构造函数 6.遍历构造函数 7.懒加载 8.只读属性 1.函数 掌握函数的定义 掌握外部参数的用处 掌握无返回类型的三种函数定义方式 ...