AOP切点切面内容

 一、实现接口MethodBeforeAdvice该拦截器会在调用方法前执行

            实现接口   AfterReturningAdvice该拦截器会在调用方法后执行

            实现接口  MethodInterceptor该拦截器会在调用方法前后都执行,实现环绕结果。

[java] view plain copy
 
  1. package com.ly.model;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.aopalliance.intercept.MethodInterceptor;  
  6. import org.aopalliance.intercept.MethodInvocation;  
  7. import org.springframework.aop.AfterReturningAdvice;  
  8. import org.springframework.aop.MethodBeforeAdvice;  
  9.   
  10. public class Advice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{  
  11.   
  12.     @Override  
  13.     public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {  
  14.         saveBeforeMessage();      
  15.     }  
  16.     @Override  
  17.     public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {  
  18.         saveAfterMessage();   
  19.     }  
  20.     public void saveBeforeMessage(){  
  21.         System.out.println("调用BeforeAdvice成功");  
  22.     }  
  23.     public void saveAfterMessage(){  
  24.         System.out.println("调用AfterAdvice成功");  
  25.     }  
  26.     @Override  
  27.     public Object invoke(MethodInvocation arg0) throws Throwable {  
  28.         System.out.println("调用RoundService之前成功");  
  29.         Object result=arg0.proceed();  
  30.         System.out.println("调用RoundService之后成功");  
  31.         return result;  
  32.     }  
  33.   
  34. }  


以下为AOPservice的实现

[java] view plain copy
 
  1. package com.ly.service.impl;  
  2.   
  3. import com.ly.service.AOPService;  
  4.   
  5. public class AOPServiceImpl implements AOPService{  
  6.     @Override  
  7.     public void print(String message) {  
  8.         System.out.println(message);  
  9.           
  10.     }  
  11.     @Override  
  12.     public void save() {  
  13.         System.out.println("保存信息成功");     
  14.     }  
  15. }  


以下为配置文件信息

[html] view plain copy
 
  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.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.              http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.              http://www.springframework.org/schema/context  
  7.              http://www.springframework.org/schema/context/spring-context.xsd">  
  8.   
  9.     <!--目标对象 -->  
  10.     <bean id="AOPservice" class="com.ly.service.impl.AOPServiceImpl">  
  11.     </bean>  
  12.     <!-- advice通知 -->  
  13.     <bean id="adviceMessage" class="com.ly.model.Advice"></bean>  
  14.       
  15.     <!-- 切入点adviser -->  
  16.     <bean id="adviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  17.         <property name="advice" ref="adviceMessage"></property>  
  18.         <!-- pattern的值使用正则表达式精确指定切入点 ,将print方法设为切入点 -->  
  19.         <property name="pattern"  
  20.             value="com.ly.service.impl.AOPServiceImpl.print"></property>  
  21.     </bean>  
  22.       
  23.       <!-- 代理对象 返回实例是目标对象 target属性指定的AOPservice对象-->  
  24.     <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">  
  25.         <!-- 设置目标(target)bean为 AOPservice,  
  26.         多个bean可以设置list集合如  
  27.         <property name="interceptorNames">  
  28.             <list>  
  29.                 <value>advisor</value>  
  30.                 <value>advisor1</value>  
  31.             </list>  
  32.         </property>-->  
  33.         <property name="target">  
  34.             <ref bean="AOPservice" />  
  35.         </property>  
  36.           
  37.         <!--源码内固定的属性private String[] interceptorNames;  -->  
  38.         <property name="interceptorNames">  
  39.             <value>adviser</value>  
  40.         </property>  
  41.     </bean>  
  42. </beans>  


以下为测试类

[java] view plain copy
 
  1. package com.ly.control;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7. import org.springframework.core.io.ClassPathResource;  
  8. import org.springframework.core.io.FileSystemResource;  
  9. import org.springframework.core.io.Resource;  
  10. import org.springframework.core.io.UrlResource;  
  11.   
  12. import com.ly.service.AOPService;  
  13.   
  14. public class SpringTest {  
  15.     public static void main(String[] args) throws IOException {  
  16.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"springBeans.xml"});  
  17.         AOPService aopservice=  (AOPService) applicationContext.getBean("proxyService");  
  18.   
  19.         //由于在springBeans.xml中只配置了 value="com.ly.service.impl.AOPServiceImpl.print"适配print方法,没有适配save方法,故只有调用print方法时才会执行advice通知  
  20.         aopservice.print("调用print成功==========》");  
  21.         aopservice.save();  
  22.     }  
  23. }  



二、在web应用中调用时需要用WebApplicationContext这个来得到Spring配置中的bean类

    

[java] view plain copy
 
    1. public class UserAction extends ActionSupport {  
    2.     private static final Logger log = Logger.getLogger(UserAction.class);  
    3.   
    4.     private UserService userService;  
    5.   
    6.     public UserService getUserService(){  
    7.         if(userService == null){  
    8.             WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getRequest().getServletContext());  
    9.             userService = (UserService) applicationContext.getBean("proxyService");  
    10. //          userService = new UserServiceImpl();  
    11.         }  
    12.           
    13.         return userService;  
    14.     }  
    15.       
原文地址:https://www.cnblogs.com/wxj-106/p/7886687.html