Spring AOP 入门实例 一

1:导入所需的spring jar包:

Spring AOP 入门实例 一_第1张图片

 2:在Src目录下新增applicationContext.xml配置文件

       



    
    
    
    
    
    
    	
        
        
        
        
        
             
        	  
        	  
        	   
        	  
        
        
    

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

你可能感兴趣的:(Spring AOP 入门实例 一)