aspectj的切入点语法定义细节

 简单介绍下表达式应该怎样写,假如只拦截返回值类型是String应该怎么写?
beans.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.         <aop:aspectj-autoproxy/>  
  11.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>  
  12.         <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>  
  13.         <aop:config>  
  14.             <aop:aspect id="asp" ref="aspetbean">  
  15.                 <aop:pointcut id="mycut" expression="execution(java.lang.String cn.itcast.service.impl.PersonServiceBean.*(..))"/>  
  16.                 <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
  17.                 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  
  18.                 <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  
  19.                 <aop:after pointcut-ref="mycut" method="doAfter"/>  
  20.                 <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
  21.             </aop:aspect>  
  22.         </aop:config>  
  23. </beans>  



PersonServiceBean.java

Java代码 复制代码
  1. package cn.itcast.service.impl;   
  2.   
  3. import cn.itcast.service.PersonService;   
  4.   
  5. public class PersonServiceBean implements PersonService {   
  6.   
  7.     public String getPersonName(Integer id) {   
  8.         System.out.println("我是getPersonName()方法");   
  9.         return "xxx";   
  10.     }   
  11.   
  12.     public void save(String name) {   
  13.         //throw new RuntimeException("我爱例外");   
  14.         System.out.println("我是save()方法");   
  15.     }   
  16.   
  17.     public void update(String name, Integer id) {   
  18.         System.out.println("我是update()方法");   
  19.     }   
  20.   
  21. }  
package cn.itcast.service.impl;

import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {

	public String getPersonName(Integer id) {
		System.out.println("我是getPersonName()方法");
		return "xxx";
	}

	public void save(String name) {
		//throw new RuntimeException("我爱例外");
		System.out.println("我是save()方法");
	}

	public void update(String name, Integer id) {
		System.out.println("我是update()方法");
	}

}


那么只有getPersonName方法会被拦截到,因为save和update返回值类型都是void,
SpringAOPTest.java

Java代码 复制代码
  1. package junit.test;   
  2. import org.junit.BeforeClass;   
  3. import org.junit.Test;   
  4. import org.springframework.context.ApplicationContext;   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  6.   
  7. import cn.itcast.service.PersonService;   
  8.   
  9. public class SpringAOPTest {   
  10.   
  11.     @BeforeClass  
  12.     public static void setUpBeforeClass() throws Exception {   
  13.     }   
  14.   
  15.     @Test public void interceptorTest(){   
  16.         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");   
  17.         PersonService personService = (PersonService)cxt.getBean("personService");   
  18.         personService.save("yyy");   
  19.     }   
  20. }  
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)cxt.getBean("personService");
		personService.save("yyy");
	}
}


运行单元测试代码,控制台输出
我是save()方法
说明,没有被拦截到
修改SpringAOPTest.java

Java代码 复制代码
  1. package junit.test;   
  2.   
  3. import org.junit.BeforeClass;   
  4. import org.junit.Test;   
  5. import org.springframework.context.ApplicationContext;   
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  7.   
  8. import cn.itcast.service.PersonService;   
  9.   
  10. public class SpringAOPTest {   
  11.   
  12.     @BeforeClass  
  13.     public static void setUpBeforeClass() throws Exception {   
  14.     }   
  15.   
  16.     @Test public void interceptorTest(){   
  17.         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");   
  18.         PersonService personService = (PersonService)cxt.getBean("personService");   
  19.         personService.getPersonName(2);   
  20.     }   
  21. }  
package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)cxt.getBean("personService");
		personService.getPersonName(2);
	}
}


运行单元测试代码,控制台输出:
前置通知
进入方法
我是getPersonName()方法
后置通知
最终通知
退出方法

说明,被拦截到了

加入现在要求,输入参数的第一个参数是String类型,后面不管有没有参数,该如何写呢?
beans.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.         <aop:aspectj-autoproxy/>  
  11.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>  
  12.         <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>  
  13.         <aop:config>  
  14.             <aop:aspect id="asp" ref="aspetbean">  
  15.                 <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(java.lang.String,..))"/>  
  16.                 <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
  17.                 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  
  18.                 <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  
  19.                 <aop:after pointcut-ref="mycut" method="doAfter"/>  
  20.                 <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
  21.             </aop:aspect>  
  22.         </aop:config>  
  23. </beans>  


现在就匹配save和update这两个方法,运行单元测试代码,控制台输出
我是getPersonName()方法
说明并没有被拦截到

SpringAOPTest.java

Java代码 复制代码
  1. package junit.test;   
  2.   
  3.   
  4. import org.junit.BeforeClass;   
  5. import org.junit.Test;   
  6. import org.springframework.context.ApplicationContext;   
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  8.   
  9. import cn.itcast.service.PersonService;   
  10.   
  11. public class SpringAOPTest {   
  12.   
  13.     @BeforeClass  
  14.     public static void setUpBeforeClass() throws Exception {   
  15.     }   
  16.   
  17.     @Test public void interceptorTest(){   
  18.         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");   
  19.         PersonService personService = (PersonService)cxt.getBean("personService");   
  20.         personService.save("yyy");   
  21.     }   
  22. }  
package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)cxt.getBean("personService");
		personService.save("yyy");
	}
}


运行单元测试代码,控制台输出:
前置通知
进入方法
我是save()方法
后置通知
最终通知
退出方法

说明被拦截到了

换成personService.update("yyy", 9);控制台输出
前置通知
进入方法
我是update()方法
后置通知
最终通知
退出方法

说明也能被拦截到

现在问题是:要求拦截所有非void返回值的方法(就是说只要不返回void,你返回什么我都不关心),怎么写呢?
beans.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.         <aop:aspectj-autoproxy/>  
  11.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>  
  12.         <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>  
  13.         <aop:config>  
  14.             <aop:aspect id="asp" ref="aspetbean">  
  15.                 <aop:pointcut id="mycut" expression="execution(!void cn.itcast.service.impl.PersonServiceBean.*(..))"/>  
  16.                 <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
  17.                 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  
  18.                 <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  
  19.                 <aop:after pointcut-ref="mycut" method="doAfter"/>  
  20.                 <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
  21.             </aop:aspect>  
  22.         </aop:config>  
  23. </beans>  


运行单元测试代码,控制台输出:
我是update()方法
修改SpringAOPTest.java

Java代码 复制代码
  1. package junit.test;   
  2.   
  3.   
  4. import org.junit.BeforeClass;   
  5. import org.junit.Test;   
  6. import org.springframework.context.ApplicationContext;   
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  8.   
  9. import cn.itcast.service.PersonService;   
  10.   
  11. public class SpringAOPTest {   
  12.   
  13.     @BeforeClass  
  14.     public static void setUpBeforeClass() throws Exception {   
  15.     }   
  16.   
  17.     @Test public void interceptorTest(){   
  18.         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");   
  19.         PersonService personService = (PersonService)cxt.getBean("personService");   
  20.         personService.getPersonName(9);   
  21.     }   
  22. }  
package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)cxt.getBean("personService");
		personService.getPersonName(9);
	}
}


运行单元测试代码,控制台输出:
前置通知
进入方法
我是getPersonName()方法
后置通知
最终通知
退出方法


这些都是我们在写AOP表达式的时候,经常使用的一些技术点。
如果我们要对cn.itcast.service包底下的所有类,包括子包底下的所有类,进行拦截的话,那该怎样定义呢?
beans.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.         <aop:aspectj-autoproxy/>  
  11.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>  
  12.         <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>  
  13.         <aop:config>  
  14.             <aop:aspect id="asp" ref="aspetbean">  
  15.                 <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>  
  16.                 <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
  17.                 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  
  18.                 <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  
  19.                 <aop:after pointcut-ref="mycut" method="doAfter"/>  
  20.                 <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
  21.             </aop:aspect>  
  22.         </aop:config>  
  23. </beans>  


运行单元测试代码,控制台输出:
前置通知
进入方法
我是getPersonName()方法
后置通知
最终通知
退出方法

说明也能拦截到

那么大家要注意的是,我们定义完了这个表达式之后,它是为满足表达式里的所有对象都创建代理对象,它创建的代理对象是怎样创建的?在前面已经给大家展示过了,如果Spring发现里面的类它实现了接口,那么它就采用JDK的创建动态代理技术来创建代理对象;如果Spring发现被拦截的类没有实现接口的话,那么它就采用cglib的方式来产生代理对象,这些都在前面给大家介绍过了,这些也是Spring内部使用AOP的一些实现过程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值