java 代理機(jī)制的實(shí)例詳解
java 代理機(jī)制的實(shí)例詳解
前言:
java代理分靜態(tài)代理和動態(tài)代理,動態(tài)代理有jdk代理和cglib代理兩種,在運(yùn)行時生成新的子類class文件。本文主要練習(xí)下動態(tài)代理,代碼用于備忘。對于代理的原理和機(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異常的時候調(diào)用 * @param pjp * @throws Throwable */ @Around("anyMethod()") public void invoke(ProceedingJoinPoint pjp) throws Throwable{ pjp.proceed(); } }
以上就是java代理機(jī)制的實(shí)例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
mybatisplus報(bào)Invalid bound statement (not found)錯誤的解決方法
搭建項(xiàng)目時使用了mybatisplus,項(xiàng)目能夠正常啟動,但在調(diào)用mapper方法查詢數(shù)據(jù)庫時報(bào)Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2020-08-08Springboot配置文件內(nèi)容加密代碼實(shí)例
這篇文章主要介紹了Springboot配置文件內(nèi)容加密代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11IDEA的spring項(xiàng)目使用@Qualifier飄紅問題及解決
這篇文章主要介紹了IDEA的spring項(xiàng)目使用@Qualifier飄紅問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11SpringBoot-RestTemplate如何實(shí)現(xiàn)調(diào)用第三方API
這篇文章主要介紹了SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08