对aop进行了小测试,动手才能看清真相!
首先,写个aop中的target对象,
package com.xll.aop;
public class Target {
//...
public void work(){
System.out.println("我属于Target类且在Target中工作!");
}
//...
}
现在我写个target2:
package com.xll.aop;
public class Target2 {
public void work(){
System.out.println("work in the Target2.....");
}
}
然后写上我需要织入的advise(不知道这么描述是否正确,advise理解成我需要的横切关注点):
package com.xll.aop;
public class Advise {
public void weaveBefore(){
System.out.println("----before----");
}
public void weaveAfter(){
System.out.println("----after----");
}
}
然后在spring的主配置文件applicationContext.xml中配置:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="myTarget" class="com.xll.aop.Target"></bean>
<bean id="myTarget2" class="com.xll.aop.Target2"></bean>
<bean id="myAdvise" class="com.xll.aop.Advise"></bean>
<aop:config>
<aop:aspect id="myAspect1" ref="myAdvise" ><!--ref为要切入的功能模块Aspect -->
<aop:pointcut id="myPoint"
expression="execution(* com.xll.aop.Target.*(..))"/>
<aop:before
pointcut-ref="myPoint"
method="weaveBefore"/> <!-- method是切入的功能模块Aspect中的方法 -->
</aop:aspect>
<aop:aspect id="myAspect2" ref="myAdvise" >
<aop:pointcut id="myPoint2"
expression="execution(* com.xll.aop.Target2.work(..))"/>
<aop:after
pointcut-ref="myPoint2"
method="weaveAfter"/>
</aop:aspect>
</aop:config>
</beans>
配置好后,编写我的单元测试:
package com.xll.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import junit.framework.TestCase;
public class TestAop extends TestCase{
public void testAop(){
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Target target = (Target)ctx.getBean("myTarget");
target.work();
}
}
测试结果:
----before----
我属于Target类且在Target中工作!
好了,织入成功!
现在修改我的测试:
public void testAop(){
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Target2 target = (Target2)ctx.getBean("myTarget2");
target.work();
}
结果如下:
work in the Target2.....
----after----
对于AOP思想的理解,以后再补上,网上很多资源。
分享到:
相关推荐
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
源代码是spring-boot添加aop切面功能的demo,简单易懂.
Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
赠送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;...
java中Spring配置的AOP切面不生效的解决方案
aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等
springIOC与AOP配置
Spring中的AOP可以控制权限,在我们做项目的时候,用Struts的过滤方法也可以实现。 当然这个文件只是提供测试,我们主要是学习这个AOP的思想。大家下载后可以再摸索......
Spring aop Spring aop
spring aop测试项目
跟开涛学习SpringAop之后的一些测试代码,也是这篇帖子测试代码的补充https://my.oschina.net/longfong/blog/805410
springAop与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....
springAOP详解
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
spring package, aop function
spring aop
从Spring5-AOP的概念->底层原理->相关术语->AOP操作(AspectJ注解)->AOP操作(AspectJ配置文件)说明AOP的相关笔记