循序渐进之Spring AOP(3) - 配置代理
上一篇介绍了几种Advice(增强),并通过代码演示了生成代理的方式,下面来看通过配置文件配置方式把Advice织入目标类。
注意,配置文件方式仍然不是spring AOP的最好方式,学习配置方式也是为了循序渐进的掌握内核技术。
接口SmartCar
- public interface SmartCar {
- void lock(String userName);
- }
实现类MyCar
- public class MyCar implements SmartCar {
- @Override
- public void lock(String userName) {
- System.out.println(userName + "锁车");
- }
- }
定义了两个增强,一个在锁车方法前执行,提示检查窗户是否关闭,一个在锁车方法后执行,"哔哔"声提示锁车成功
- public class BeforeAdviceDemo implements MethodBeforeAdvice {
- @Override
- public void before(Method method, Object[] args, Object obj)
- throws Throwable {
- System.out.println("请检查窗户是否关好");
- }
- }
- public class AfterAdviceDemo implements AfterReturningAdvice {
- @Override
- public void afterReturning(Object returnObj, Method method, Object[] args,
- Object obj) throws Throwable {
- System.out.println("哔哔");
- }
- }
applicationContext.xml: 用配置文件来声明代理
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <bean id="beforeAdvice" class="demo.aop.BeforeAdviceDemo" />
- <bean id="afterAdvice" class="demo.aop.AfterAdviceDemo" />
- <bean id="target" class="demo.aop.MyCar" />
- <bean id="myCar" class="org.springframework.aop.framework.ProxyFactoryBean"
- p:target-ref="target"
- p:proxyTargetClass="true">
- <property name="interceptorNames">
- <list>
- <idref local="beforeAdvice" />
- <idref local="afterAdvice" />
- </list>
- </property>
- </bean>
- </beans>
测试代码
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("demo/aop/applicationContext.xml");
- SmartCar myCar = (SmartCar)context.getBean("myCar");
- myCar.lock("Tom");
- }
执行结果
- 请检查窗户是否关好
- Tom锁车
- 哔哔
配置文件中首先声明了3个bean,分别是增强的目标类和两个增强,没有特别的配置内容。重点是ProxyFactoryBean的配置,来看看它可配置的属性和用法。
| target | 目标对象,ref另一个bean |
| proxyTargetClass | 是否对类进行代理(而不是对接口),true或false,设置为true时,使用CGLib代理技术 |
| interceptorNames | 织入目标对象的Advice(实现了Advisor或者MethodInterceptor接口的bean) |
| proxyInterfaces | 代理所要实现的接口,如果指定了proxyTargetClass=true,此属性会被忽略 |
| optimize | 设置为true时,强制使用CGLib代理技术 |
下面再分别增加一个环绕增强和一个异常抛出增强。
先修改MyCar类,使它能够抛出异常,粗心的约翰会忘记锁车而抛出一个异常
- public class MyCar implements SmartCar {
- @Override
- public void lock(String userName) {
- if(userName.equals("Careless John")) {
- throw new RuntimeException(userName + "忘记锁车");
- } else {
- System.out.println(userName + "锁车");
- }
- }
- }
环绕增强类
- public class AroundAdviceDemo implements MethodInterceptor {
- @Override
- public Object invoke(MethodInvocation invocation) throws Throwable {
- Object[] args = invocation.getArguments();
- String name = (String)args[0];
- System.out.print("Hey, " + name);
- Object obj = invocation.proceed(); //调用目标方法
- System.out.println("Goodbye! " + name);
- return obj;
- }
- }
异常抛出增强
- public class ThrowsAdviceDemo implements ThrowsAdvice {
- public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
- System.out.println("捕获异常:" + ex.getMessage());
- System.out.println("(报警)滴滴滴滴滴滴滴滴滴滴滴滴");
- }
- }
applicationContext.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <bean id="beforeAdvice" class="demo.aop.BeforeAdviceDemo" />
- <bean id="afterAdvice" class="demo.aop.AfterAdviceDemo" />
- <bean id="aroundAdvice" class="demo.aop.AroundAdviceDemo" />
- <bean id="throwsAdvice" class="demo.aop.ThrowsAdviceDemo" />
- <bean id="target" class="demo.aop.MyCar" />
- <bean id="myCar" class="org.springframework.aop.framework.ProxyFactoryBean"
- p:target-ref="target"
- p:proxyTargetClass="true">
- <property name="interceptorNames">
- <list>
- <idref local="aroundAdvice" />
- <idref local="throwsAdvice" />
- <idref local="beforeAdvice" />
- <idref local="afterAdvice" />
- </list>
- </property>
- </bean>
- </beans>
测试代码
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("demo/aop/applicationContext.xml");
- SmartCar myCar = (SmartCar)context.getBean("myCar");
- myCar.lock("Tom");
- System.out.println("-------------------------");
- myCar.lock("Careless John");
- }
运行结果
- Hey, Tom请检查窗户是否关好
- Tom锁车
- 哔哔
- Goodbye! Tom
- -------------------------
- Hey, Careless John请检查窗户是否关好
- 捕获异常:Careless John忘记锁车
- (报警)滴滴滴滴滴滴滴滴滴滴滴滴
除了上面的四种增强,还有一种特殊的引介增强(Introduction),在下一篇中单独介绍
循序渐进之Spring AOP(3) - 配置代理的更多相关文章
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 基于注解的Spring AOP的配置和使用--转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- Spring AOP 和 动态代理技术
AOP 是什么东西 首先来说 AOP 并不是 Spring 框架的核心技术之一,AOP 全称 Aspect Orient Programming,即面向切面的编程.其要解决的问题就是在不改变源代码的情 ...
- Spring AOP 不同配置方式产生的冲突问题
Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...
- 深入理解Spring AOP之二代理对象生成
深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...
- spring AOP为什么配置了没有效果?
spring Aop的配置一定要配置在springmvc配置文件中 springMVC.xml 1 <!-- AOP 注解方式 :定义Aspect --> <!-- ...
- spring aop注解配置
spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...
- spring aop + xmemcached 配置service层缓存策略
Memcached 作用与使用 基本介绍 1,对于缓存的存取方式,简言之,就是以键值对的形式将数据保存在内存中.在日常业务中涉及的操作无非就是增删改查.加入缓存机制后,查询的时候,对数据进行缓存,增删 ...
- spring---aop(5)---Spring AOP的配置的背后的配置
写在前面 Spring AOP中Pointcut,dvice 和 Advisor三个概念 1)切入点 Pointcut 在介绍Pointcut之前,有必要先介绍 Join Point(连接点)概念. ...
随机推荐
- intellij idea svn使用一 导入、更新、提交、解决冲突
大体上是转载,针对版本14有一些特殊的添加. 查看svn的资源库: 下面的多出了一个svn的窗口,在左边有加号可以添加一个svn的库 输入svn的地址,我用的是本地的测试,所以地址为svn://127 ...
- JMeter3.0脚本中文乱码解决方法
修改apache-jmeter-3.0\bin\jmeter.properties文件,编辑jsyntaxtextarea.font.family=宋体.
- 【java】Date与String之间的转换及Calendar类:java.text.SimpleDateFormat、public Date parse(String source) throws ParseException和public final String format(Date date)
package 日期日历类; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util. ...
- BCryptPasswordEncoder加密及判断密码是否相同
项目中用到了BCryptPasswordEncoder对密码进行二次加密,需要注意的是,加密后的字符串比较长,数据库的长度至少为60位. 通过BCryptPasswordEncoder的加密的相同字符 ...
- 如何在阿里云linux上部署java项目
前2天把git练了下,敲了很多命令,也借助图形界面增强自己的理解,乘着余热把linux在熟悉下.然后想起以前婷主有让我帮忙搭建的阿里云服务器,所以就想自己试着在阿里云的linux上搭建自己的jav ...
- Foreign websites
[社交] 1.Twitter. It's what's happening. 2.Telegram Messenger 3.Facebook - Log In or Sign Up 4.Instagr ...
- Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对
给定一个数组,其中每个元素出现两次,除了一对(两个元素).找到这个唯一对的元素. 输入:第一行输入包含一个表示测试用例数的整数T.然后T测试用例如下.每个测试用例由两行组成.每个测试用例的第一行包含整 ...
- 78、excel的读写操作
本篇主要是用python来自动生成excel数据文件也就是简单的excel读写操作.python读写excel文件主要是第三方模块库xlrd.xlwt. 本篇导航: 写excel 读excel 一.写 ...
- 最短的IE判断var ie=!-[1,]分析
以前最短的IE判定借助于IE不支持垂直制表符的特性搞出来的. 复制代码代码如下: var ie = !+"\v1"; 仅仅需要7bytes!参见这篇文章,<32 byte ...
- linux编译php gd扩展
1 安装gd的依赖包 yum -y install gd gd2 gd-devel gd2-devel zlib freetype 2 安装jpeg: wget http://www.ijg.org/ ...