第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已。
3.7.1一般增强的使用
public class Target {
public void say(){
System.out.println("say...");
}
public String getName(int id,String name){
System.out.println("getName...");
return "MR"+name+id;
}
public void around(){
System.out.println("around...");
}
public void targetThrow(){
System.out.println("targetThrow");
throw new RuntimeException("我是一个运行期异常...");
}
}
b、
POJO(增强所在的类
)
public class Pojo {
public void before() {
System.out.println("前置增强");
}
public void afterReturning(String retName, int id, String name) {
System.out.println("后置增强,返回值为:"+retName+" 入参为:"+id+"-"+name);
}
public Object around(ProceedingJoinPoint point) throws Throwable{
System.out.println("方法执行之前");
Object object = point.proceed();
System.out.println("方法执行之后");
return object;
}
public void throwEx(Exception ex){
System.out.println("抛出异常增强,异常信息:"+ex.getMessage());
}
public void finalEx(){
System.out.println("Final增强");
}
}aop
命名空间与
Schema
方式配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 目标类 -->
<bean id="target" class="cn.framelife.spring.schema.Target"></bean> <!-- 增强所在的类 -->
<bean id="advisor" class="cn.framelife.spring.schema.Pojo"></bean> <!-- 配置基于schema的切面 -->
<aop:config proxy-target-class="true">
<!-- 确定增强所在类,并引入 -->
<aop:aspect ref="advisor">
<!—
前置增强
method是配置增强所在类中的方法
-->
<aop:before method="before" pointcut="target(cn.framelife.spring.schema.Target)"/> <!—
后置增强
Returning 是返回值,必须和method中的参数名是一样的
在Schema配置中,多个切点函数的与操作是and,或操作是or
-->
<aop:after-returning method="afterReturning" pointcut="execution(* cn.framelife.spring.schema..getName(..)) and args(id,name)" returning="retName" arg-names="retName,id,name"/> <!-- 环绕增强 -->
<aop:around method="around" pointcut="execution(* cn.framelife.spring.schema..around(..))"/> <!—
抛出异常增强
throwing是异常对象,必须和method中的参数是一样的
-->
<aop:after-throwing method="throwEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))" throwing="ex"/> <!-- Final增强 -->
<aop:after method="finalEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))"/>
</aop:config>
</beans>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Target target = (Target) context.getBean("target");
target.say();
target.getName(10, "Zhang");
target.around();
target.targetThrow();
e、结果
前置增强
say...
前置增强
getName...
后置增强,返回值为:MRZhang10 入参为:10-Zhang
前置增强
方法执行之前
around...
方法执行之后
前置增强
targetThrow
抛出异常增强,异常信息:我是一个运行期异常...
Final增强
Exception in thread "main" java.lang.RuntimeException: 我是一个运行期异常...
3.7.2引介增强的使用
我们还是使用3.6.2@DeclareParents中的例子:Waiter为目标类,然后让目标类拥有ISeller接口的功能:
http://blog.csdn.net/p_3er/article/details/9269407
a、两个接口与两个类
目标类与其接口:
- public interface IWaiter {
- public void service();
- }
- @Component
- public class Waiter implements IWaiter {
- @Override
- public void service() {
- System.out.println("service");
- }
- }
运行期织入到目标类的功能类与其接口:
- public interface ISeller {
- public void sell();
- }
- public class Seller implements ISeller {
- @Override
- public void sell() {
- System.out.println("sell");
- }
- }
b、配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 目标类 -->
<bean id="waiter" class="cn.framelife.spring.schema.Waiter"></bean> <!-- 增强所在的类 -->
<bean id="advisor" class="cn.framelife.spring.schema.Pojo"></bean> <aop:config proxy-target-class="true">
<!—
虽然引介增强不需要在增强所在的类中定义一个方法用于增强的实现,但<aop:aspect ref="advisor">中的ref属性依然要指定一个增强Bean
--> <aop:aspect ref="advisor">
<!—
引介增强
types-matching 目标类
implement-interface 要织入目标类的接口
default-impl 织入接口的实现类
-->
<aop:declare-parents
types-matching="cn.framelife.spring.schema.IWaiter+"
implement-interface="cn.framelife.spring.schema.ISeller"
default-impl="cn.framelife.spring.schema.Seller"/>
</aop:aspect>
</aop:config>
</beans>
c、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
IWaiter waiter = (IWaiter) context.getBean("waiter");
waiter.service();
ISeller seller = (ISeller)waiter;
seller.sell();
d、结果
service
sell
第三章 AOP 基于Schema的AOP的更多相关文章
- spring aop 基于schema的aop
AOP的基本概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP ...
- 开涛spring3(6.3) - AOP 之 6.3 基于Schema的AOP
6.3 基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...
- spring3: 基于Schema的AOP
6.3 基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...
- 基于Schema的AOP 配置使用详解
原文地址:http://jinnianshilongnian.iteye.com/blog/1418598 基于Schema的AOP从Spring2.0之后通过"aop"命名空间来 ...
- Spring5参考指南:基于Schema的AOP
文章目录 基于Schema的AOP 定义Aspect 定义Pointcut 定义Advice advice参数 Advisors 基于Schema的AOP 上篇文章我们讲到了使用注解的形式来使用Spr ...
- spring的aop 基于schema
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程 一 前期工作 1.新建一个java项目,我是使用的maven,所以我新建了一个简单的maven项目,因为mav ...
- Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut
pointcut(切断点)表达式: execution(public * *(..)) execution(* set*(..)) execution(* com.xyz.service.Accoun ...
- 第三章 AOP 基于@AspectJ的AOP
在前面,我们分别使用Pointcut.Advice.Advisor接口来描述切点.增强.切面.而现在我们使用@AdpectJ注解来描述. 在下面的例子中,我们是使用Spring自动扫描和管理Bean. ...
- Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect
Spring所有的切面和通知器都必须放在一个<aop:config>内(可以配置包含多个<aop:config>元素),每个<aop:config>包含pointc ...
随机推荐
- Visual Studio 控件命名规范(很详细)
VS 控件命名规范 Type Prefix Example Array arr arrShoppingList Boolean bln blnIsPostBack Byte byt bytPixelV ...
- Delphi代码中嵌入ASM代码(简单明了)
前言 Delphi作为一个快速高效的开发平台,使用的人越来越多,但熟悉在Delphi代码中嵌入ASM代码的程序员我想不多,因为这方面的资料太少了,另一方面,它还需要有基本的汇编语言知识,关於汇编语言的 ...
- php数组转xml的递归实现
原文:php数组转xml的递归实现 PHP中奖数组转为xml的需求是常见的,而且实现方法也有很多种,百度找了一下各种实现方法,但是基本是借组一些组件啥的.我就自己写了一个字符串拼组的方法,支持多维数组 ...
- HTML5动态分页效果代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Nginx和Tomcat负载均衡实现session共享(转)
以前的项目使用Nginx作为反向代理实现了多个Tomcat的负载均衡,为了实现多个Tomcat之间的session共享,使用了开源的Memcached-Session-Manager框架. 此框架的优 ...
- 【Demo 0007】导航控制器
本章学习要点 1. 了解导航控制器基本知识: 2. 掌握导航控制器基本用法: 3. 掌握导航控制器基本用法;
- java web从零单排第十六期《struts2》控制标签(2)
1.s:subset标签概述: s:subset标签功能是从一个集合中取出部分元素合并成一个新的集合,新生成的这个集合是原来集合的子集.属性和意义如下: 属性名 是否必需 默认值 类型 说明介绍 co ...
- 基于飞思卡尔i.MX 6Quad Sabrelite开发板的触摸屏调试
1 概述 本次任务是在飞思卡尔i.MX 6Quqd Sabrelite开发板上调试触屏驱动,触屏芯片是Goodix的gt828芯片,触屏接口是I2C. 操作系统:android 4.0.4 ...
- 辛星PHP教程之yii和ci教程已经写完,望与朋友们交流
记得有个朋友给我说,你写的PHP框架是不是过于少了.我感觉仅仅有一个thinkphp确实不好,于是就又写了下yii和ci的教程,事实上我之前是研究过这两个框架的,因此写起来也还算得心应手吧.估计接下 ...
- 18.如何自我Struts2它Struts2标签和综合汇总文章有点早
18.如何自我Struts2它Struts2标签和综合汇总文章有点早[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了.仅仅好传到百度云上: h ...