基于Schema配置切面
1、一个简单切面的配置
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:before pointcut="target(com.yyq.schema.NaiveWaiter) and execution(* greetTo(..))"
method="preGreeting"/>
</aop:aspect>
</aop:config>
<bean id="adviceMethods" class="com.yyq.schema.AdviceMethods"/>
<bean id="naiveWaiter" class="com.yyq.schema.NaiveWaiter"/>
<bean id="naughtyWaiter" class="com.yyq.schema.NaughtyWaiter"/>
</beans>
使用一个<aop:aspect>元素标签定义切面,其内部可以定义多个增强。在<aop:config>元素中可以定义多个切面。通过<aop:before>声明了一个前置增强,并通过pointcut属性定义切点表达式,切点表达式的语法和@AspectJ中所用的语法完全相同,由于&&在XML中使用不便,所以一般用and操作符代替。通过method属性指定增强的方法,该方法应该是adviceMethods Bean中的方法。
package com.baobaotao.schema;
import org.aspectj.lang.ProceedingJoinPoint; public class AdviceMethods {
public void preGreeting(String name) {
System.out.println("--how are you!--");
System.out.println(name);
}
//后置增强对应方法
public void afterReturning(int retVal){
System.out.println("----afterReturning()----");
System.out.println("returnValue:"+retVal);
System.out.println("----afterReturning()----");
}
//环绕增强对应方法
public void aroundMethod(ProceedingJoinPoint pjp){
System.out.println("----aroundMethod()----");
System.out.println("args[0]:"+pjp.getArgs()[0]);
System.out.println("----aroundMethod()----");
}
//抛出异常增强
public void afterThrowingMethod(IllegalArgumentException iae){
System.out.println("----afterThrowingMethod()----");
System.out.println("exception msg:"+iae.getMessage());
System.out.println("----afterThrowingMethod()----");
}
//final增强
public void afterMethod(){
System.out.println("----afterMethod()----");
} //------------绑定连接点参数----------//
public void bindParams(int num,String name){
System.out.println("----bindParams()----");
System.out.println("name:"+name);
System.out.println("num:"+num);
System.out.println("----bindParams()----");
}
}
NaiveWaiter类:
package com.yyq.schema;
public class NaiveWaiter implements Waiter {
@Override
public void greetTo(String name) {
System.out.println("NaiveWaiter:greet to " + name + "...");
}
@Override
public void serveTo(String name) {
System.out.println("NaiveWaiter:serving to " + name + "...");
}
public void smile(String clientName,int times){
System.out.println("NaiveWaiter:smile to "+clientName+ times+"times...");
}
}
NaughtyWaiter类:
package com.yyq.schema;
public class NaughtyWaiter implements Waiter {
public void greetTo(String clientName) {
System.out.println("NaughtyWaiter:greet to " + clientName + "...");
}
public void serveTo(String clientName) {
System.out.println("NaughtyWaiter:serving " + clientName + "...");
}
public void joke(String clientName, int times) {
System.out.println("NaughtyWaiter:play " + times + " jokes to " + clientName + "...");
}
}
测试方法:
package com.yyq;
import com.yyq.schema.Waiter;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SchemaTest {
@Test
public void schemaTest(){
String configPath = "com\\yyq\\schema\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter naiveWaiter = (Waiter)ctx.getBean("naiveWaiter");
Waiter naughtyWaiter = (Waiter)ctx.getBean("naughtyWaiter");
naiveWaiter.greetTo("John");
naughtyWaiter.greetTo("Tom");
}
}
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:pointcut id="greetToPointcut" expression="target(com.yyq.schema.NaiveWaiter) and execution(* greetTo(..))"/>
<aop:before method="preGreeting" pointcut-ref="greetToPointcut"/>
</aop:aspect>
</aop:config>
使用<aop:pointcut>定义了一个切点,并通过id属性进行命名,通过pointcut-ref引用这个命名的切点。和<aop:before>一样,除了引介增强外,其他任意增强类型都拥有pointcut、pointcut-ref和method这3个属性。
<aop:config proxy-target-class="true">
<aop:pointcut id="greetToPointcut2" expression="target(com.yyq.schema.NaiveWaiter) and execution(* greetTo(..))"/>
<aop:aspect ref="adviceMethods">
<aop:before method="preGreeting" pointcut-ref="greetToPointcut2"/>
</aop:aspect>
<aop:aspect ref="adviceMethods">
<aop:after method="postGreeting" pointcut-ref="greetToPointcut2"/>
</aop:aspect>
</aop:config>
3、各种增强类型的配置
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:after-returning method="afterReturning"
pointcut="target(com.baobaotao.SmartSeller)" returning="retVal" />
</aop:aspect>
</aop:config>
2)环绕增强
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:around method="aroundMethod"
pointcut="execution(* serveTo(..)) and within(com.baobaotao.Waiter)" />
</aop:aspect>
</aop:config>
3)抛出异常增强
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:after-throwing method="afterThrowingMethod"
pointcut="target(com.baobaotao.SmartSeller) and execution(* checkBill(..))"
throwing="iae" />
</aop:aspect>
</aop:config>
4)Final增强
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:after method="afterMethod"
pointcut="execution(* com..*.Waiter.greetTo(..))" />
</aop:aspect>
</aop:config>
5)引介增强
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:declare-parents
implement-interface="com.baobaotao.Seller"
default-impl="com.baobaotao.SmartSeller"
types-matching="com.baobaotao.Waiter+" />
</aop:aspect>
</aop:config>
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:before method="bindParams"
pointcut="target(com.yyq.schema.NaiveWaiter) and args(name,num,..)"/>
</aop:aspect>
</aop:config>
AdviceMethods绑定参数的增强方法:
//------------绑定连接点参数----------//
public void bindParams(int num,String name){
System.out.println("----bindParams()----");
System.out.println("name:"+name);
System.out.println("num:"+num);
System.out.println("----bindParams()----");
}
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="testAdvice" pointcut="execution(* com..*.Waiter.greetTo(..))"/>
</aop:config>
<bean id="testAdvice" class="com.yyq.schema.TestBeforeAdvice"/>
增强类:
package com.yyq.schema;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class TestBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("------TestBeforeAdvice------");
System.out.println("clientName:"+args[0]);
System.out.println("------TestBeforeAdvice------");
}
}
基于Schema配置切面的更多相关文章
- Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面
1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...
- 基于@AspectJ注解配置切面与基于XML配置切面
1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...
- 第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...
- Spring 基于XML配置
基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
- 基于Schema的AOP 配置使用详解
原文地址:http://jinnianshilongnian.iteye.com/blog/1418598 基于Schema的AOP从Spring2.0之后通过"aop"命名空间来 ...
- 基于@AspectJ配置Spring AOP之一--转
原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
- 开涛spring3(6.3) - AOP 之 6.3 基于Schema的AOP
6.3 基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...
- spring aop 基于schema的aop
AOP的基本概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP ...
随机推荐
- python之面向对象的程序设计
一.什么是面向对象的程序设计 面向过程的程序设计的核心是过程,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了程序的复杂度 缺点是: ...
- hdu6024 Building Shops(区间dp)
https://cn.vjudge.net/problem/HDU-6024 分开考虑某一点种与不种,最后取二者的最小值. dp[i][1] = min(dp[i-1][0], dp[i-1][1]) ...
- 阿里云CentOS 7无外网IP的ECS访问外网(配置网关服务器)
说明: 1.必须要有一台机器具有外网IP的ECS. 2.如果不想配置具有外网IP的ECS时,可以购买NAT网关,但需要钱,贵.下面会说明NAT网关的配置. 3.最后吐槽一下阿里云VPC网关导致不能按照 ...
- grid - 通过网格线名称设置网格项目位置
使用网格线名称设置网格项目位置和使用网格线号码设置网格项目位置类似. 1.引用网格线名称的时候不应该带方括号 <view class="grid"> <view ...
- CentOS7配置MySQL5.7主备
1:主库设置(1)修改配置文件vi /etc/my.cnf[mysqld]log-bin=master-binserver-id=1 (2)创建用户#mysql -u root -pmysql> ...
- TensorFlow保存和载入模型
首先定义一个tf.train.Saver类: saver = tf.train.Saver(max_to_keep=1) 其中,max_to_keep参数设定只保存最后一个参数,默认值是5,即保存最后 ...
- 动态绑定事件到特定dom元素上,包含新增加的
$('body').on('click', 'a.detail-data', function (e) { //动态事件绑定 为body元素下所有的a.detail-data元素添加一个事件 包括新增 ...
- Effective Java 第三版—— 85. 其他替代方式优于Java本身序列化
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- vue前端知识点整理
1. 说一下Vue的双向绑定数据的原理 vue 实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty() 来劫持各个属性的 setter, ...
- css 定位(fixed > absolute > relative)与层级zIndex 的权限认知
原则1: fixed > absolute > relative原则2: zIndex 越高越牛逼,不管你是谁无视身份.原则3: 青出于蓝而胜于蓝,儿子永远比父亲强原则4: 平台很重要. ...