1:导入所需的spring jar包:
2:在Src目录下新增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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!--需要被嵌入切面的类-->
<bean id="dogBean" class="com.aopProject.testBLL.DogAction"/>
<!--切面类-->
<bean id="aspectBean" class="com.aopProject.aspectJ.AnimalActionAspect"/>
<!--proxy-target-class:true,cglib动态代理模式;false:jdk动态代理模式-->
<aop:config proxy-target-class="true">
<!--<aop:pointcut id="pointcutRun" expression="execution(public void com.aopProject.testBLL.DogAction.run())"/>
<aop:pointcut id="pointcutJump" expression="execution(public void com.aopProject.testBLL.DogAction.jump())"/>-->
<!--切入点(个人觉得其实可以理解为一个匹配函数的正则表达式)-->
<aop:pointcut id="pointcutRun" expression="execution(* com.aopProject.testBLL.*.run(..))"/>
<aop:pointcut id="pointcutJump" expression="execution(* com.aopProject.testBLL.*.jump(..))"/>
<!--切面;要指明所依赖的切面类(ref="aspectBean"),里面嵌套advice(Before,After returning,After throwing,Around,After )-->
<aop:aspect id="animalRun" ref="aspectBean">
<!--每一个advice都要指定一个切入点(pointcut-ref="pointcutRun")和要切入的函数(method="beforeRunAction")-->
<aop:before pointcut-ref="pointcutRun" method="beforeRunAction"/>
<aop:after-returning pointcut-ref="pointcutRun" method="afterRunAction"/>
<aop:before pointcut-ref="pointcutJump" method="beforeJumpAction"/>
<aop:after-returning pointcut-ref="pointcutJump" method="afterJumpAction"/>
</aop:aspect>
</aop:config>
</beans>
3:配置junit参考博文
http://www.blogjava.net/qileilove/archive/2014/09/11/417823.html
4:编写动物run和jump的行为
public interface AnimalAction {
public void run();
public void jump();
}
public class DogAction implements AnimalAction{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("dog start runing");
}
@Override
public void jump() {
// TODO Auto-generated method stub
System.out.println("dog start jumpping");
}
}
5:编写aspect类和advice方法
public class AnimalActionAspect {
public void beforeRunAction()
{
System.out.println("run order has been sent");
}
public void afterRunAction()
{
System.out.println("run action ended");
}
public void beforeJumpAction()
{
System.out.println("jump order has been sent");
}
public void afterJumpAction()
{
System.out.println("jump action ended");
}
}
6:获取applicationcontext文件对象并反射出bean对象
@Test
public void testRun() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DogAction da = (DogAction)ctx.getBean("dogBean");
da.run();
}
@Test
public void testJump() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DogAction da = (DogAction)ctx.getBean("dogBean");
da.jump();
}
7:最后给出源代码