zoukankan      html  css  js  c++  java
  • spring3: Aspectj后置返回通知

    Aspectj后置返回通知

    接口:

    package chapter1.server;
    
    public interface IHelloService2 {
    	public int sayAfterReturning(String param);
    }
    

      

    接口实现

    package chapter1.service.impl;
    
    import chapter1.server.IHelloService2;
    
    public class HelloService2 implements IHelloService2 {
    
    	public int sayAfterReturning(String param) {
    		// TODO Auto-generated method stub
    		System.out.println("============ say after returning:" + param);
    		return 1;
    	}
    
    }
    

      

    配置:

    一定要加:<aop:aspectj-autoproxy/> 启动对Aspectj的支持

    <aop:aspectj-autoproxy/>
    <bean id="helloService" class="chapter1.service.impl.HelloService2" />
    <bean id="aspect" class="chapter1.aop.HelloAspect2"/>
    

      

    AOP切面:

    一定要引入:org.aspectj.lang.annotation.Aspect; 否则不执行

    package chapter1.aop;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.annotation.AfterReturning;
    
    @Aspect
    public class HelloAspect2 {
    
    	
    	
    	//方法一
    	//通知
    	@AfterReturning(	
    			//value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",
    			value="execution(* chapter1..*.sayAfterReturning(..))",			
    			argNames="retVal", 
    			returning="retVal")
    	public void afterReturningAdvice(Object retVal)
    	{
    		System.out.println("================= return after advice : " + retVal);
    	}
    	
    	
    	//方法二
    	//定义切入点
    	@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")
    	public void returnPointcut(String param) {}
    	
    	public void afterReturningAdvice2(Object retVal)
    	{
    		
    	}
    	
    }
    

      

    测试程序:

    @Test
    	public void testAspectAfterReturning()
    	{
    		ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");
    		IHelloService2 hello = context.getBean("helloService", IHelloService2.class);
    		hello.sayAfterReturning("hahah");
    	}
    

      

    结果:

    ============ say after returning:hahah
    ================= return after advice : 1

  • 相关阅读:
    js自执行函数的几种不同写法的比较
    chrome浏览器font-size<12px无效解决办法
    清楚浮动的那些事
    css中font-family的中文字体
    雅虎34条军规
    Modernizr的介绍和使用
    手机也能拍大片
    响应式Web设计 – 布局
    JAVA基础-JDBC连接池
    JAVA基础-JDBC使用
  • 原文地址:https://www.cnblogs.com/achengmu/p/8574911.html
Copyright © 2011-2022 走看看