java 代理機制的實例詳解
更新時間:2017年08月24日 15:35:11 投稿:lqh
這篇文章主要介紹了java 代理機制的實例詳解的相關資料,這里說明下如何實現(xiàn)代理機制,幫助大家理解掌握這部分內容,需要的朋友可以參考下
java 代理機制的實例詳解
前言:
java代理分靜態(tài)代理和動態(tài)代理,動態(tài)代理有jdk代理和cglib代理兩種,在運行時生成新的子類class文件。本文主要練習下動態(tài)代理,代碼用于備忘。對于代理的原理和機制,網(wǎng)上有很多寫的很好的,就不班門弄斧了。
jdk代理
實例代碼
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異常的時候調用
* @param pjp
* @throws Throwable
*/
@Around("anyMethod()")
public void invoke(ProceedingJoinPoint pjp) throws Throwable{
pjp.proceed();
}
}
以上就是java代理機制的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
mybatisplus報Invalid bound statement (not found)錯誤的解決方法
搭建項目時使用了mybatisplus,項目能夠正常啟動,但在調用mapper方法查詢數(shù)據(jù)庫時報Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2020-08-08
IDEA的spring項目使用@Qualifier飄紅問題及解決
這篇文章主要介紹了IDEA的spring項目使用@Qualifier飄紅問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot-RestTemplate如何實現(xiàn)調用第三方API
這篇文章主要介紹了SpringBoot-RestTemplate實現(xiàn)調用第三方API的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

