Spring学习(七)——增强类
Spring 切点
什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物。但在这为数从多的连接点中,如何定位到某个感兴趣的连接点上呢?AOP通过"切点"定位特定连接点。通过数据库查询的概念来理解切点和连接点的关系再适合不过了;连接点相当于数据库中的记录,而切点相当于查询条件.
在Spring中,切点通过org.springframework.aop.Pointcut接口进行描述,它使用类和方法作为连接点的查询条
件,Spring
AOP的规则解析引擎负责解析切点所设定的查询条件,找到对应的连接点---其实确切地说,应该是执行点而非连接点,因为连接点是方法执行前、执行后等包
括方位信息的具体程序执行点,而切点只定位到某个方法上,所以如果希望定位到具体连接点上,还需要提供方位信息。
aop术语:
1、切面:所有切入点的集合
2、切入点:一组符合某种规则的连接点
3、连接点:狭义上通俗的讲指的是某个方法
4、通知:在某个连接点上的某种操作,该操作并非连接点中的操作,而是外来的操作。
5、引入(Introduction):引入(在AspectJ中被称为inter-type声明)使得一个切面可以定义被通知对象实现给定的接口, 并且可以为那些对象提供具体的实现
- 前置增强
- 后置增强
- 环绕增强
- 异常抛出增强
- 引介增强
package com.paic.zhangqi.spring.aop;public interface Waiter { void greetTo(String name); void serveTo(String name);}package com.paic.zhangqi.spring.aop;public class NaiveWaiter implements Waiter { @Override public void greetTo(String name) { System.out.println(greet to +name+...); } @Override public void serveTo(String name) { System.out.println(serving +name+...); }}package com.paic.zhangqi.spring.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetingBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object obj) throws Throwable { String clientName = (String)args[0]; System.out.println(How are you!Mr.+clientName+.); }}后置增强类GreetingAfterAdvice.java 在目标类方法调用后执行
package com.paic.zhangqi.spring.aop; import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice; public class GreetingAfterAdvice implements AfterReturningAdvice { @Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println(Please enjoy yourself!);
}
}
测试类 TestAdvice.java
package com.paic.zhangqi.spring.aop; import org.springframework.aop.AfterAdvice;
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.framework.ProxyFactory; public class TestAdvice { public static void main(String[] args) { Waiter target = new NaiveWaiter(); BeforeAdvice beforeAdvice = new GreetingBeforeAdvice();
AfterAdvice afterAdvice = new GreetingAfterAdvice(); // spring 提供的代理工厂
ProxyFactory pf = new ProxyFactory(); // 设置代理目标
pf.setTarget(target); // 为代理目标添加增强
pf.addAdvice(beforeAdvice);
pf.addAdvice(afterAdvice); // 生成代理实例
Waiter proxy = (Waiter)pf.getProxy(); proxy.greetTo(John);
proxy.serveTo(Tomcat); }
}
输出结果
How are you!Mr.John.
greet to John...
Please enjoy yourself!
How are you!Mr.Tomcat.
serving Tomcat...
Please enjoy yourself!
使用配置文件进行配置 beans.xml
<beans beans="" http:="" schema="" spring-beans-3.0.xsd="" www.springframework.org=""
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans">
<bean class="com.paic.zhangqi.spring.aop.GreetingBeforeAdvice" id="greetingBefore" />
<bean class="com.paic.zhangqi.spring.aop.GreetingAfterAdvice" id="greetingAfter" />
<bean class="com.paic.zhangqi.spring.aop.NaiveWaiter" id="target" />
</beans>
对应的测试类 SpringConfigTest.java
package com.paic.zhangqi.spring.aop; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringConfigTest { public static void main(String[] args) {
String configPath = com/paic/zhangqi/spring/aop/beans.xml;
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter waiter = (Waiter) ctx.getBean(waiter);
waiter.greetTo(John);
waiter.serveTo(Tomcat);
}
}
同样得到输出
How are you!Mr.John.
greet to John...
Please enjoy yourself!
How are you!Mr.Tomcat.
serving Tomcat...
Please enjoy yourself!
Spring学习(七)——增强类的更多相关文章
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- Spring AOP 创建增强类
AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强: 1)前置增强:org.springframework.aop.BeforeAd ...
- spring学习七 spring和dynamic project进行整合
spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...
- Spring学习(七)--Spring MVC的高级技术
一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...
- Spring学习(七)
注解产生原因 1.传统spring的做法:使用xml来对bean进行注入和或者是配置aop.事物配置文件体积庞大,造成了配置文件的可读性和可维护性很低Java文件和xml不断切换,造成思维不连贯开发效 ...
- Spring学习七:ComponentScan注解
今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1.ComponentScan注解是什么 其实很 ...
- Spring学习(七)-----Spring Bean的5种作用域
在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例(singleton) - 每个Spring IoC 容器返回一 ...
- spring学习七
一: web.xml中常用配置元素? <servlet></servlet>: 在向servlet或JSP页面制定初始化参数或定制URL时,首先命名servlet或JSP页面. ...
- (转)SpringMVC学习(七)——Controller类的方法返回值
http://blog.csdn.net/yerenyuan_pku/article/details/72511844 本文所有案例代码的编写均建立在前文SpringMVC学习(六)——SpringM ...
随机推荐
- 树莓3B+_apt-get update && apt-get upgrade
在Windows下安装软件,我们只需要有EXE文件,然后双击,下一步直接OK就可以了.但在LINUX下,不是这样的.每个LINUX的发行版,都会维护一个自己的软件仓库,我们常用的几乎所有软件都在这里面 ...
- linux查看文件命令tail的使用
一.介绍 linux tail命令用途是依照要求将指定的文件的最后部分输出到终端中,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档案内 ...
- Circos Ubuntu 安装
下载: http://www.circos.ca/software/download/circos/ Perl 需要5.8及以上的版本. 查看版本的命令: perl -v 解压安装包 安装包是一 ...
- 20155215宣言 实验四 Andoid开发基础实验报告
20155215宣言 实验四 Andoid开发基础实验报告 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 2.完成实验 ...
- JavaScript之数组的常用操作函数
js对数组的操作非常频繁,但是每次用到的时候都会被搞混,都需要去查相关API,感觉这样很浪费时间.为了加深印象,所以整理一下对数组的相关操作. 常用的函数 concat() 连接两个或更多的数组,并返 ...
- Deep Learning 教程翻译
Deep Learning 教程翻译 非常激动地宣告,Stanford 教授 Andrew Ng 的 Deep Learning 教程,于今日,2013年4月8日,全部翻译成中文.这是中国屌丝军团,从 ...
- js灵活处理日期(函实例)
基础方法: var dd = new Date() dd.getFullYear() dd.getMonth() dd.getDate() dd.getDay() //获取星期几(0~6) dd.ge ...
- jQuery File Upload 文件上传插件使用二 (功能完善)
使用Bootstrap美化进度条 Bootstrap现在几乎是人尽皆知了,根据它提供的进度条组件, 让进度条显得高大尚点 正因为其功能强大,js模块文件之间牵连较深 不好的地方耦合度非常高 重要的参数 ...
- php实现图形计算器
存档: index.php <html> <head> <title>图形计算器开发</title> <meta http-equiv=" ...
- 使用GitLab创建项目