`
8366
  • 浏览: 798785 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

第20讲--aspectj的切入点语法定义细节

阅读更多

表达式分析:

 

 

expression="execution(* cn.com.xinli.service.impl.PersionServiceBean.*(..))"

1.第一个星号表示 拦截方法的返回值为任意

 

  如果为 java.lang.String    表示只拦截 返回值为String的方法

  如果为 void                      则表示只拦截 返回值为 void 的方法

  如果为 !void                     则表示只拦截 返回值 非 void的方法

 

2. 如果我们只拦截 方法第一个参数为String,剩下的参数类型任意 则可以

 

 

expression="execution(java.lang.String cn.com.xinli.service.impl.PersionServiceBean.*(java.lang.String,..))"

 

 

未处理的问题:

 

基于配置实现aop的方式 如何给各种通知传递参数,比如给前置通知传递方法的入参,给后置通知传递方法的返回值,等晚上发了版本在研究,基于注解的已经实现了.

 

 

处理遗留问题:基于注解的方式实现在前置置通知得到拦截方法的入参,在置通知得到拦截方法的返回值,谢谢楼下的  windywindy 帮助,表示感谢!!

 

首先在beans.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:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  
			
           <aop:aspectj-autoproxy proxy-target-class="true"/>
           <!--切面和业务bean都需要交给容器管理--> 
            <bean id="myInterceptor" class="cn.com.xinli.service.MyInterceptor"></bean>
        	<bean id="personService" class="cn.com.xinli.service.impl.PersionServiceBean"></bean>
        	
        	 <aop:config>
        	 <!-- 定义一个切面 -->
        	 <aop:aspect id="asp" ref="myInterceptor">
        	 <!-- 定义切入点,定义拦截表达式,前置通知,后置通知,等.. --> 
        		<aop:pointcut id="mycut" expression="execution(* cn.com.xinli.service.impl.PersionServiceBean.*(..))and args(name)"/>
        		<aop:before pointcut-ref="mycut" method="doAccessCheck" arg-names="name"/>
        		<aop:after-returning pointcut-ref="mycut" method="doAfterReturning" returning="result"/>
			  	<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
			  	<aop:after pointcut-ref="mycut" method="doAfter"/>
			  	<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
        	</aop:aspect>
        </aop:config>
      
       
     		
	</beans>

 

 

1.前置置通知得到拦截方法的入参,关键就是在前置方法中定义一个连接点,从连接点中得到入参,如果有多个则可以用便利的方式得到。                    测试调用 ps.save("huxl");

 

public void doAccessCheck(JoinPoint joinPoint) {
		System.out.println("前置通知:");
		Object[] args = joinPoint.getArgs(); 
		
        for (int i=0; i<args.length; i++) {     
            System.out.println("入参:"+args[i]);     
        } 
       
	}

 

 

结果:

入参:huxl
进入方法
我是save()方法
后置通知中得到结果:null
最终通知
退出方法

 

2.在后置通知中得到拦截方法的返回值 关键在与给在配置文件中 给后置方法配置返回的参数,在后置方法中将返回参入当做入参传递                 测试  ps.getPersonName(1);

 

public void doAfterReturning(String result) {
		System.out.println("后置通知中得到结果:"+result);
	}

 

 

结果:

 

前置通知:
入参:1
进入方法
我是getPersonName()方法
后置通知中得到结果:xxx
最终通知
退出方法

 

分享到:
评论
1 楼 windywindy 2009-06-10  
未解决的问题让我来帮你解决吧!
哈哈~
public class XMLInterceptor {

	 public void doAccessCheck(JoinPoint joinPoint,Integer name) {  
	        System.out.println("前置通知--"+name);  
	        Object[] args = joinPoint.getArgs();  
	        for (int i=0; i<args.length; i++) {  
	            System.out.println("---"+args[i]);  
	        }  
	        System.out.println(joinPoint.getSignature().getName());  
	    }  
	  
	    public void doAfterReturning(Person person) {  
	        System.out.println("后置通知="+person.getName());  
	    }  
	      
	    public void doAfter() {  
	        System.out.println("最终通知");  
	    }
	      
	    public void doAfterThrowing() {  
	        System.out.println("例外通知");  
	    }  
	      
	    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {  
	        System.out.println("进入方法");  
	        Object result = pjp.proceed();  
	        System.out.println("退出方法");  
	        return result;  
	    }  
}


<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
          <context:component-scan base-package="com"/>
          
          <bean id="person" class="com.pojo.Person"/>
          
          <aop:aspectj-autoproxy proxy-target-class="true"/>   
           <bean id="xmlInterceptor" class="com.aop.XMLInterceptor"/>  
           <bean id="personService"     class="com.service.PersonService"/>   
           
              <aop:config>  
              <aop:pointcut id="mycut" expression="execution(* com.service..*.*(..)) "/>  
              <aop:pointcut id="argcut" expression="execution(* com.service.PersonService.*(..)) and args(name)"/>
              <aop:pointcut id="personcut" expression="execution(* com.service.PersonService.*(..)) and args(person)"/>    
             <!-- 定义一个切面 -->  
             <aop:aspect id="asp" ref="xmlInterceptor">  
                <aop:before pointcut-ref="argcut" method="doAccessCheck" arg-names="name" />  
                <aop:after-returning pointcut-ref="personcut" method="doAfterReturning" arg-names="person" />  
                <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  
                <aop:after pointcut-ref="mycut" method="doAfter" />  
                <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
            </aop:aspect>  
        </aop:config> 
</beans>

public class Person {
	private int age;

	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String string) {
		this.name = string;
	}
}
测试自己写吧!


相关推荐

Global site tag (gtag.js) - Google Analytics