java 代理機(jī)制的實(shí)例詳解
java 代理機(jī)制的實(shí)例詳解
前言:
java代理分靜態(tài)代理和動(dòng)態(tài)代理,動(dòng)態(tài)代理有jdk代理和cglib代理兩種,在運(yùn)行時(shí)生成新的子類class文件。本文主要練習(xí)下動(dòng)態(tài)代理,代碼用于備忘。對(duì)于代理的原理和機(jī)制,網(wǎng)上有很多寫的很好的,就不班門弄斧了。
jdk代理
實(shí)例代碼
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyFactory implements InvocationHandler { private Object tarjectObject; public Object creatProxyInstance(Object obj) { this.tarjectObject = obj; return Proxy.newProxyInstance(this.tarjectObject.getClass() .getClassLoader(), this.tarjectObject.getClass() .getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; if (AssessUtils.isAssess()) { result = method.invoke(this.tarjectObject, args); }else{ throw new NoAssessException("This server cannot run this service."); } return result; } }
cglib代理
import java.lang.reflect.Method; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; public class ProxyCglibFactory implements MethodInterceptor { private Object tarjectObject; public Object creatProxyInstance(Object obj) { this.tarjectObject = obj; Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(this.tarjectObject.getClass()); enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy arg3) throws Throwable { Object result = null; if (AssessUtils.isAssess()) { result = method.invoke(this.tarjectObject, args); }else{ throw new NoAssessException("This server cannot run this service."); } return result; } }
Aspect注解
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AssessInterceptor { @Pointcut(value="execution (* com..*.*(..))") private void anyMethod(){}; @Before("anyMethod()") public void doBefore(JoinPoint joinPoint) throws NoAssessException{ if (!AssessUtils.isAssess()) { throw new NoAssessException("This server cannot run this service."); } } /** * Around異常的時(shí)候調(diào)用 * @param pjp * @throws Throwable */ @Around("anyMethod()") public void invoke(ProceedingJoinPoint pjp) throws Throwable{ pjp.proceed(); } }
以上就是java代理機(jī)制的實(shí)例,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
mybatisplus報(bào)Invalid bound statement (not found)錯(cuò)誤的解決方法
搭建項(xiàng)目時(shí)使用了mybatisplus,項(xiàng)目能夠正常啟動(dòng),但在調(diào)用mapper方法查詢數(shù)據(jù)庫時(shí)報(bào)Invalid bound statement (not found)錯(cuò)誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2020-08-08Springboot配置文件內(nèi)容加密代碼實(shí)例
這篇文章主要介紹了Springboot配置文件內(nèi)容加密代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11IDEA自動(dòng)清理類中未使用的import包的操作方法
在項(xiàng)目開發(fā)中,經(jīng)常會(huì)引入很多未使用的import包,這不僅增加了編譯時(shí)間,還會(huì)使代碼可讀性變差,設(shè)置IDEA自動(dòng)清理未使用的import包,可以提高代碼的可讀性,本文給大家介紹IDEA自動(dòng)清理類中未使用的import包的方法,感興趣的朋友一起看看吧2024-09-09IDEA的spring項(xiàng)目使用@Qualifier飄紅問題及解決
這篇文章主要介紹了IDEA的spring項(xiàng)目使用@Qualifier飄紅問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11SpringBoot-RestTemplate如何實(shí)現(xiàn)調(diào)用第三方API
這篇文章主要介紹了SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08