在某些时候,我们工程中使用的JDK 不一定就是1.5 以上,也就是说可能不支持Annotation
注解,这时自然也就不能使用@AspectJ 注解驱动的AOP 了,那么如果我们仍然想使用AspectJ
灵活的切入点表达式,那么该如何呢?Spring 为我们提供了基于xml schematic 的aop 命名空间,它的使用方式和@AspectJ
注解类似,不同的是配置信息从注解中转移到了Spring 配置文件中。在这里,我们将详细介绍如何使用Spring
提供的<aop:config/> 标签来配置Spring AOP 。
1 、一点准备工作和一个例子
使用<aop:config/> 标签,需要给Spring 配置文件中引入基于xml schema 的Spring AOP
命名空间。完成后的Spring 配置文件如下(在该节,所有例程的配置文件中添加了Spring AOP
命名空间,除非特殊情况外,为了节约空间,这部分将在给出的代码中省略),粗体内容即为我们需要添加的内容
- <?
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-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd >
- ………… Spring配置信息
- </
beans
>
关于aop命名空间的标签,我们前面使用过的有<aop:aspectj-autoproxy/>,在这一节,我们将以<aop:config/>标签作为重点。事实上,我们在这一节介绍的所有标签都是该标签的子标签。
下面有一个例程来直观的展示如何使用<aop:config/>标签来配置Spring
AOP(完整代码见例程4.15)。在例子中,我们使用<aop:config/>配置一个切面并拦截目标对象Peoples的
SayHello()方法,在它执行前输出提示信息。
首先创建工程AOP_Test4.15,添加Spring IoC和Spring AOP库后,创建aop.test包,新建目标类People,代码如下:
- package
aop.test;
-
-
-
-
-
- public
class
People{
-
- public
String SayHello(String str){
- System.out.println(this
.getClass().getName()+
"说:"
+str);
- return
str;
- }
- }
修改Spring xml配置文件,将该类注册为一个受管Bean:
- <
bean
id
=
"TestBean"
class
=
"aop.test.People"
/>
创建含有main()方法的测试类TestMain,从Spring IoC容器中获取Peoples对象,并调用其SayHello()方法,代码如下:
- package
aop.test;
-
-
- public
class
TestMain {
- public
static
void
main(String[] args) {
-
- ApplicationContext ac = new
ClassPathXmlApplicationContext(
- "applicationContext.xml"
);
-
- People p = (People) ac.getBean("TestBean"
);
- p.SayHello("传入的参数值"
);
- }
- }
创建MyAspect类,添加一个beforeAdvice()方法作为前置通知方法,代码如下:
- package
aop.test;
-
- import
org.aspectj.lang.JoinPoint;
-
- public
class
MyAspect {
-
- public
void
beforeAdvice(JoinPoint point) {
- System.out.println("前置通知被触发:"
+
- point.getTarget().getClass().getName()+
- "将要"
+ point.getSignature().getName());
- }
- }
修改xml配置文件,为其添加aop命名空间,并把MyAspect注册为一个受管Bean,作为我们下面定义切面的backing bean。代码如下:
- <?
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-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
-
- <
bean
id
=
"MyAspect"
class
=
"aop.test.MyAspect"
/>
- <
bean
id
=
"TestBean"
class
=
"aop.test.People"
/>
-
- <
aop:config
proxy-target-class
=
"true"
>
- <
aop:aspect
ref
=
"MyAspect"
order
=
"0"
id
=
"Test"
>
- <
aop:pointcut
id
=
"testPointcut"
- expression
=
"execution(* aop..*(..))"
/>
- <
aop:before
pointcut-ref
=
"testPointcut"
- method
=
"beforeAdvice"
/>
- </
aop:aspect
>
- </
aop:config
>
- </
beans
>
运行主类,输出如下:
分享到:
相关推荐
java springAOP 事务+注释 带全部jar包! 即下即用!
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式
赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...
Spring 权限 这里是一个 spring aop 实现的一个权限 包括权限设计 这个不是很能懂 包括注释
赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...
赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...
赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...
Spring AOP--日志管理,注释齐全,欢迎大家共同交流。
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...
赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...
一阶段 1、Spring概述 2、一切从bean开始 ...Spring AOP的涉及原理及具体实践 SpringJDBC的涉及原理及二次开发 SpringMVC框架设计原理及手写实现 四阶段 Spring事务源码解析 需要其他源码请私信我
赠送jar包:spring-aop-5.3.7.jar; 赠送原API文档:spring-aop-5.3.7-javadoc.jar; 赠送源代码:spring-aop-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.7.pom; 包含翻译后的API文档:spring-aop...
赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....
spring注解方式实现aop,希望对大家有所帮助,内附注释