Spring AOP 入门实例 一

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:最后给出源代码

http://download.csdn.net/detail/cjsy_2011/9710187

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值