共計 3365 個字符,預計需要花費 9 分鐘才能閱讀完成。
本篇內容介紹了“Sping aop 面向切面編程通知的方法是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
建議先看看:java 靜態代理和動態代理
一、創建兩個接口:
接口 TestServiceInter:
package com.hubin.aop;
public interface TestServiceInter { public void sayHello();
}
接口 TestServiceInter2:
package com.hubin.aop;
public interface TestServiceInter2 { public void sayBye();
}
二、創建被代理類 Test1Service 該類實現了上面的兩個接口
package com.hubin.aop;
*
*
* 被代理類,相當于我自己干事情
* @author Administrator
*
*/
public class Test1Service implements TestServiceInter,TestServiceInter2 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello() {
// TODO Auto-generated method stub
System.out.println(hi +name);
}
public void sayBye() {
// TODO Auto-generated method stub
System.out.println(bye +name);
}
}
三、創建前置通知 MyMethodBeforeAdvice 類
package com.hubin.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
* 前置通知類,類似于中介公司實現我交代的事情之前做的一些事情
* @author Administrator
*
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
/**
* method: 被調用方法名字
* args: 給 method 傳遞的參數
* target: 目標對象
*/
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println( 前置通知被調用
}
}
四、創建后置通知類 MyMethodAfterAdvice:
package com.hubin.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
* 后置通知
* @author Administrator
*
*/
public class MyMethodAfterAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println( 后置通知被調用
}
}
五、創建環繞通知類 MyMethodAroundAdvice:
package com.hubin.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stub
System.out.println( 調用環繞通知前
arg0.proceed();// 這句話不調用的話,被代理類的方法不會運行
System.out.println( 調用環繞通知后
return null;
}
*/
public class MyMethodThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Method m,Object []obj,Object target,Exception e){ System.out.println( 出事了 +e.getMessage());
}
}
七、創建配置文件 beans.xml(文件位置在包 com/hubin/aop/)
?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:p= http://www.springframework.org/schema/p
xsi:schemaLocation= http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
!-- 配置被代理的對象 --
bean id= test1Service > 八、隨便創建一個類用來做測試
package com.hubin.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext( com/hubin/aop/beans.xml
TestServiceInter ts=(TestServiceInter) ac.getBean( proxyFactoryBean
ts.sayHello();
//((TestServiceInter2)ts).sayBye();
}
}
九、運行結果:
前置通知被調用
調用環繞通知前
hi 王大錘
調用環繞通知后
后置通知被調用
前置通知被調用
調用環繞通知前
bye 王大錘
調用環繞通知后
后置通知被調用
“Sping aop 面向切面編程通知的方法是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!
正文完