AOP面向切面程式設計MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor介面的使用
阿新 • • 發佈:2019-02-13
一、實現介面MethodBeforeAdvice該攔截器會在呼叫方法前執行
實現介面 AfterReturningAdvice該攔截器會在呼叫方法後執行
實現介面 MethodInterceptor該攔截器會在呼叫方法前後都執行,實現環繞結果。
package com.ly.model; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; public class Advice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{ @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { saveBeforeMessage(); } @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { saveAfterMessage(); } public void saveBeforeMessage(){ System.out.println("呼叫BeforeAdvice成功"); } public void saveAfterMessage(){ System.out.println("呼叫AfterAdvice成功"); } @Override public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("呼叫RoundService之前成功"); Object result=arg0.proceed(); System.out.println("呼叫RoundService之後成功"); return result; } }
以下為AOPservice的實現
package com.ly.service.impl;
import com.ly.service.AOPService;
public class AOPServiceImpl implements AOPService{
@Override
public void print(String message) {
System.out.println(message);
}
@Override
public void save() {
System.out.println("儲存資訊成功");
}
}
以下為配置檔案資訊
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--目標物件 --> <bean id="AOPservice" class="com.ly.service.impl.AOPServiceImpl"> </bean> <!-- advice通知 --> <bean id="adviceMessage" class="com.ly.model.Advice"></bean> <!-- 切入點adviser --> <bean id="adviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="adviceMessage"></property> <!-- pattern的值使用正則表示式精確指定切入點 ,將print方法設為切入點 --> <property name="pattern" value="com\.ly\.service\.impl\.AOPServiceImpl\.print"></property> </bean> <!-- 代理物件 返回例項是目標物件 target屬性指定的AOPservice物件--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 設定目標(target)bean為 AOPservice, 多個bean可以設定list集合如 <property name="interceptorNames"> <list> <value>advisor</value> <value>advisor1</value> </list> </property>--> <property name="target"> <ref bean="AOPservice" /> </property> <!--原始碼內固定的屬性private String[] interceptorNames; --> <property name="interceptorNames"> <value>adviser</value> </property> </bean> </beans>
以下為測試類
package com.ly.control;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import com.ly.service.AOPService;
public class SpringTest {
public static void main(String[] args) throws IOException {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"springBeans.xml"});
AOPService aopservice= (AOPService) applicationContext.getBean("proxyService");
//由於在springBeans.xml中只配置了 value="com\.ly\.service\.impl\.AOPServiceImpl\.print"適配print方法,沒有適配save方法,故只有呼叫print方法時才會執行advice通知
aopservice.print("呼叫print成功==========》");
aopservice.save();
}
}
二、在web應用中呼叫時需要用WebApplicationContext這個來得到Spring配置中的bean類
public class UserAction extends ActionSupport {
private static final Logger log = Logger.getLogger(UserAction.class);
private UserService userService;
public UserService getUserService(){
if(userService == null){
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getRequest().getServletContext());
userService = (UserService) applicationContext.getBean("proxyService");
// userService = new UserServiceImpl();
}
return userService;
}