简单介绍下表达式应该怎样写,假如只拦截返回值类型是String应该怎么写?
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/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(java.lang.String cn.itcast.service.impl.PersonServiceBean.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <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>
PersonServiceBean.java
- 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()方法");
- }
- }
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
- 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");
- }
- }
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
- 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);
- }
- }
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 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/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(java.lang.String,..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <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>
现在就匹配save和update这两个方法,运行单元测试代码,控制台输出
我是getPersonName()方法
说明并没有被拦截到
SpringAOPTest.java
- 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");
- }
- }
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 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/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(!void cn.itcast.service.impl.PersonServiceBean.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <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>
运行单元测试代码,控制台输出:
我是update()方法
修改SpringAOPTest.java
- 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);
- }
- }
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 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/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <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>
运行单元测试代码,控制台输出:
前置通知
进入方法
我是getPersonName()方法
后置通知
最终通知
退出方法
说明也能拦截到
那么大家要注意的是,我们定义完了这个表达式之后,它是为满足表达式里的所有对象都创建代理对象,它创建的代理对象是怎样创建的?在前面已经给大家展示过了,如果Spring发现里面的类它实现了接口,那么它就采用JDK的创建动态代理技术来创建代理对象;如果Spring发现被拦截的类没有实现接口的话,那么它就采用cglib的方式来产生代理对象,这些都在前面给大家介绍过了,这些也是Spring内部使用AOP的一些实现过程