使用Spring 2.0新特性实现前置通知--基于Schema方式
来源:互联网 发布:arm uclinux linux 编辑:程序博客网 时间:2024/05/20 10:12
在使用Spring 2.0 Schema方式的aop实现中,我们的通知,不再需要实现Spring的接口,如:
import org.aspectj.lang.JoinPoint;
//不需要在实现MethodBeforeAdvise
public class LogBeforeAdvice ...{
public void before(JoinPoint joinPoint)...{
System.out.println("log before service");
}
}
为了使用Spring 2.0基于XML Schema的AOP设置方式,您必须在XML设档的开头加入AOP的names
<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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
加入相应的namespace后,就可以使用spring的aop标签了
<!-- 配置文件Schemal需要改变 -->
<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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 加入aop名称空间后,就可以使用spring2.0的标签aop了 -->
<bean id="logBeforeAdvise" class="AOP2Schema.LogBeforeAdvice"/>
<bean id="helloSpeaker" class="AOP2Schema.HelloSpeaker"/>
<aop:config>
<aop:aspect id="logging" ref="logBeforeAdvise">
<aop:before pointcut="execution(* AOP2Schema.ISpeaker.* (..))" method="before"/>
</aop:aspect>
</aop:config>
</beans>
服务接口及实现:
public class HelloSpeaker implements ISpeaker ...{
public void speak() ...{
System.out.println("speak");
}
public void say() ...{
System.out.println("say");
}
}
package AOP2Schema;
public interface ISpeaker ...{
public void speak();
public void say();
}
对上面的aop标签解释一下:
首先可以看到,不需要明确声明ProxyFactoryBean了,所有AOP配置是在aop:confg标签中设置的,aop:spect标签定义Aspect的实现,也就是advise
<aop:before>标签表示设置Advice将作为Before Advise,“pointcut”属性定义PointCut表达式,以上定义了三个部:传回值,方法名和参数类型
* 表示任何返回值类型都符合
AOP2Schema.ISpeaker.* 表示ISpeaker接口里的所有方法都复合
(..) 表示任何参数类型声明符合
我们只要在测试代码中直接获取helloSpeaker,spring就可以自动按配置建立代理对象,并调用相应的advise
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring...{
public static void main(String args[]) throws Exception...{
ApplicationContext ctx=new ClassPathXmlApplicationContext("AOP2Schema/applicationContext.xml");
ISpeaker speaker=(ISpeaker)ctx.getBean("helloSpeaker");
speaker.speak();
speaker.say();
}
}
测试结果:
log before service
speak
log before service
say
- 使用Spring 2.0新特性实现前置通知--基于Schema方式
- 使用Spring 2.0新特性实现前置通知--基于Annotation方式
- 使用Spring 2.0 新特性实现声明式事务管理-基于XML Schema
- 使用Spring 2.0 新特性实现声明式事务管理-基于XML Schema
- Spring 基于注解,实现 前置通知、后置通知、返回通知、 异常通知 、后置通知
- 使用Spring 2.0 新特性实现声明式事务管理-基于Annotation
- AspectJ 基于注解的实现前置通知
- Spring AOP-前置通知
- Spring--AOP-前置通知
- Spring AOP-通知-前置通知
- 使用Spring AOP前置通知实现提高方法调用的安全性能
- 黑马程序员--Spring Aop 面向切面编程,实现前置通知
- spring中前置通知与后置通知
- Spring AOP前置通知和后置通知
- Spring前置通知与后置通知
- iOS8新特性之基于地理位置的消息通知UILocalNotification
- iOS8新特性之基于地理位置的消息通知UILocalNotification
- iOS8新特性之基于地理位置的消息通知UILocalNotification
- 用jfreechat编写图形化统计数据程序(实例)
- Linux下软件安装详解
- symbian下如何截图
- C#,.Net经典面试题
- Undo和Redo机制
- 使用Spring 2.0新特性实现前置通知--基于Schema方式
- Linux磁盘文件系统与维护
- c#连接数据库大全
- 几个关于高效工作的建议
- WINXP 运行命令集锦
- 找份工作真的那么难吗?
- 一个超级简单的dwr配置文件,介绍了dwr最常用的几个标签.
- VB6读取文本,并将每行固定的字段赋给变量
- 设计原则