Spring3系列: Spring AOP——Pointcut,Advisor

Bertha 。 2022-07-13 12:22 381阅读 0赞
  1. 为尊重别人的劳动成果,本文转自:[Spring3系列10- Spring AOP——PointcutAdvisor][Spring3_10- Spring AOP_Pointcut_Advisor]

在Spring AOP Advice例子中,Class(CustomerService)中的全部method都被自动的拦截了。但是大多情况下,你只需要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它允许你根据method的名字去拦截指定的method。另外,一个Pointcut必须结合一个Advisor来使用。

在Spring AOP中,有3个常用的概念,Advices、Pointcut、Advisor,解释如下,

Advices:表示一个method执行前或执行后的动作。

Pointcut:表示根据method的名字或者正则表达式去拦截一个method。

Advisor:Advice和Pointcut组成的独立的单元,并且能够传给proxy factory 对象。

下边来回顾一下上一篇例子中的代码

CustomerService.java

复制代码

  1. package com.lei.demo.aop.advice;
  2. public class CustomerService {
  3. private String name;
  4. private String url;
  5. public void setName(String name) {
  6. this.name = name;
  7. }
  8. public void setUrl(String url) {
  9. this.url = url;
  10. }
  11. public void printName() {
  12. System.out.println("Customer name : " + this.name);
  13. }
  14. public void printURL() {
  15. System.out.println("Customer website : " + this.url);
  16. }
  17. public void printThrowException() {
  18. throw new IllegalArgumentException();
  19. }
  20. }

复制代码

配置文件Spring-AOP-Advice.xml:

复制代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  6. <property name="name" value="LeiOOLei" />
  7. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  8. </bean>
  9. <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
  10. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  11. <property name="target" ref="customerService" />
  12. <property name="interceptorNames">
  13. <list>
  14. <value>hijackAroundMethodBean</value>
  15. </list>
  16. </property>
  17. </bean>
  18. </beans>

复制代码

HijackAroundMethod.java

复制代码

  1. package com.lei.demo.aop.advice;
  2. import java.util.Arrays;
  3. import org.aopalliance.intercept.MethodInterceptor;
  4. import org.aopalliance.intercept.MethodInvocation;
  5. public class HijackAroundMethod implements MethodInterceptor {
  6. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  7. System.out.println("Method name : "
  8. + methodInvocation.getMethod().getName());
  9. System.out.println("Method arguments : "
  10. + Arrays.toString(methodInvocation.getArguments()));
  11. // 相当于 MethodBeforeAdvice
  12. System.out.println("HijackAroundMethod : Before method hijacked!");
  13. try {
  14. // 调用原方法,即调用CustomerService中的方法
  15. Object result = methodInvocation.proceed();
  16. // 相当于 AfterReturningAdvice
  17. System.out.println("HijackAroundMethod : After method hijacked!");
  18. return result;
  19. } catch (IllegalArgumentException e) {
  20. // 相当于 ThrowsAdvice
  21. System.out.println("HijackAroundMethod : Throw exception hijacked!");
  22. throw e;
  23. }
  24. }
  25. }

复制代码

运行如下App.java

复制代码

  1. package com.lei.demo.aop.advice;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class App {
  5. public static void main(String[] args) {
  6. ApplicationContext appContext = new ClassPathXmlApplicationContext(
  7. new String[] { "Spring-AOP-Advice.xml" });
  8. System.out.println("使用Spring AOP 如下");
  9. CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
  10. System.out.println("*************************");
  11. cust.printName();
  12. System.out.println("*************************");
  13. cust.printURL();
  14. System.out.println("*************************");
  15. try {
  16. cust.printThrowException();
  17. } catch (Exception e) {
  18. }
  19. }
  20. }

复制代码

运行结果:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Method name : printURL

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer website : http://www.cnblogs.com/leiOOlei/

HijackAroundMethod : After method hijacked!

*************************

Method name : printThrowException

Method arguments : []

HijackAroundMethod : Before method hijacked!

HijackAroundMethod : Throw exception hijacked!

上边的结果中,CustomerService.java中,全部的method方法全部被拦截了,下边我们将展示怎样利用Pointcuts只拦截printName()。

你可以用名字匹配法和正则表达式匹配法去匹配要拦截的method。

1. Pointcut——Name match example

通过pointcut和advisor拦截printName()方法。

创建一个NameMatchMethodPointcut的bean,将你想拦截的方法的名字printName注入到属性mappedName,如下

  1. <bean id="customerPointcut"
  2. class="org.springframework.aop.support.NameMatchMethodPointcut">
  3. <property name="mappedName" value="printName" />
  4. </bean>

创建一个DefaultPointcutAdvisor的advisor bean,将pointcut和advice关联起来。

  1. <bean id="customerAdvisor"
  2. class="org.springframework.aop.support.DefaultPointcutAdvisor">
  3. <property name="pointcut" ref="customerPointcut" />
  4. <property name="advice" ref=" hijackAroundMethodBean " />
  5. </bean>

更改代理的interceptorNames值,将上边的advisor( customerAdvisor)替代原来的hijackAroundMethodBean。

复制代码

  1. <bean id="customerServiceProxy"
  2. class="org.springframework.aop.framework.ProxyFactoryBean">
  3. <property name="target" ref="customerService" />
  4. <property name="interceptorNames">
  5. <list>
  6. <value>customerAdvisor</value>
  7. </list>
  8. </property>
  9. </bean>

复制代码

所有的配置文件如下:

复制代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  6. <property name="name" value="LeiOOLei" />
  7. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  8. </bean>
  9. <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
  10. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  11. <property name="target" ref="customerService" />
  12. <property name="interceptorNames">
  13. <list>
  14. <value>customerAdvisor</value>
  15. </list>
  16. </property>
  17. </bean>
  18. <bean id="customerPointcut"class="org.springframework.aop.support.NameMatchMethodPointcut">
  19. <property name="mappedName" value="printName" />
  20. </bean>
  21. <bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
  22. <property name="pointcut" ref="customerPointcut" />
  23. <property name="advice" ref=" hijackAroundMethodBean " />
  24. </bean>
  25. </beans>

复制代码

再运行一下App.java,输出结果如下:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Customer website : http://www.cnblogs.com/leiOOlei/

*************************

以上运行结果显示,只拦截了printName()方法。

注意:

以上配置中pointcut和advisor可以合并在一起配置,即不用单独配置customerPointcutcustomerAdvisor,只要配置customerAdvisor时class选择NameMatchMethodPointcutAdvisor如下:

  1. <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  2. <property name="mappedName" value="printName" />
  3. <property name="advice" ref="hijackAroundMethodBean" />
  4. </bean>

这样,整个配置文件如下:

复制代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  6. <property name="name" value="LeiOOLei" />
  7. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  8. </bean>
  9. <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
  10. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  11. <property name="target" ref="customerService" />
  12. <property name="interceptorNames">
  13. <list>
  14. <value>customerAdvisor</value>
  15. </list>
  16. </property>
  17. </bean>
  18. <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  19. <property name="mappedName" value="printName" />
  20. <property name="advice" ref="hijackAroundMethodBean" />
  21. </bean>
  22. </beans>

复制代码

  实际上这种做法将method名字与具体的advice捆绑在一起,有悖于Spring松耦合理念,如果将method名字单独配置成pointcut(切入点),advice和pointcut的结合会更灵活,使一个pointcut可以和多个advice结合。

2. Pointcut——Regular exxpression match example

你可以配置用正则表达式匹配需要拦截的method,如下配置

复制代码

  1. <bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  2. <property name="patterns">
  3. <list>
  4. <value>.*URL.*</value>
  5. </list>
  6. </property>
  7. <property name="advice" ref="hijackAroundMethodBeanAdvice" />
  8. </bean>

复制代码

现在,你可以拦截名字中包含URL字符的method了,在实际工作中,你可以用它来管理DAO层,例如,你可以用“.*DAO.*”来拦截所有DAO层中的相关业务。

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 3Spring Boot系列之Session

    用 session来保存用户信息,通常会保存在服务器中(如tomcat),但是如果将应用搭建成分布式的集群,然后利用LVS或Nginx做负载均衡,那么来自同一用户的Http请求