當前位置:首頁 » 招商代理 » spring的自動代理加盟
擴展閱讀
古今奶茶加盟費多少 2025-06-20 04:39:02
微商代理加盟成人用品 2025-06-20 04:34:12

spring的自動代理加盟

發布時間: 2022-01-14 05:20:37

1. spring的BeanNameAutoProxyCreator自動創建事務代理的小問題

事務一般是指資料庫事務吧。當然不是一個方法一個事物,有的方法也許根本不需要事務。

2. springcloud 如何創建自己的動態代理

最近需要需要統計每一個節點memcache的命中、並發、超時情況。
memcache client選擇的xmemcached
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.0.0</version></dependency>

3. spring的動態代理有幾種實現方式

JAVA 代理實現

代理的實現分動態代理和靜態代理,靜態代理的實現是對已經生成了的JAVA類進行封裝。

動態代理則是在運行時生成了相關代理累,在JAVA中生成動態代理一般有兩種方式。


JDK自帶實現方法

JDK實現代理生成,是用類 java.lang.reflect.Proxy, 實現方式如下

EX:

publicclassJDKProxy{
(finalObjectc){
returnProxy.newProxyInstance(c.getClass().getClassLoader(),c.getClass().getInterfaces(),//JDK實現動態代理,但JDK實現必須需要介面
newInvocationHandler(){
publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)throwsThrowable{
//TODOAuto-generatedmethodstub
ObjectreObj=null;
System.out.print("yousay:");
reObj=method.invoke(c,args);
System.out.println("["+Calendar.getInstance().get(Calendar.HOUR)+":"
+Calendar.getInstance().get(Calendar.MINUTE)+""
+Calendar.getInstance().get(Calendar.SECOND)+"]");
returnreObj;
}
});
}
}

測試代理類方法

publicclassTestForPoxy{
publicstaticvoidmain(String[]args){
ServiceTestservice=newServiceTestImpl();
System.out.println(service.getClass().getSimpleName());
ServiceTestpoxyService=(ServiceTest)JDKProxy.getPoxyObject(service);
System.out.println(poxyService.getClass().getSuperclass());
poxyService.saySomething("hello,MyQQcodeis107966750.");
poxyService.saySomething("what'syourname?");
poxyService.saySomething("onlyfortest,hehe.");
}
}

1, Proxy實現代理的目標類必須有實現介面

2, 生成出來的代理類為介面實現類,和目標類不能進行轉換,只能轉為介面實現類進行調用

明顯特點:通過此方法生成出來的類名叫做 $Proxy0


用CGLIB包實現

CGLIB是一個開源項目,官方網址是:http://cglib.sourceforge.net/,可以去上面下載最新JAR包,

本項目用的是cglib-3.0.jar

本項目還加入了依賴JAR包asm-4.0.jar,asm-util-4.0.jar

實現方式如下

EX:

publicclassCGLIBProxy{
(Objectc){
Enhancerenhancer=newEnhancer();
enhancer.setSuperclass(c.getClass());
enhancer.setCallback(newMethodInterceptor(){
publicObjectintercept(Objectarg0,Methodarg1,Object[]arg2,MethodProxyproxy)throwsThrowable{
System.out.print("yousay:");
proxy.invokeSuper(arg0,arg2);
System.out.println("["+Calendar.getInstance().get(Calendar.HOUR)+":"
+Calendar.getInstance().get(Calendar.MINUTE)+""+Calendar.getInstance().get(Calendar.SECOND)
+"]");
returnnull;
}
});
returnenhancer.create();
}
}

測試代理類方法

publicclassTestForPoxy{
publicstaticvoidmain(String[]args){
ServiceTestservice=newServiceTestImpl();
System.out.println(service.getClass().getSimpleName());
//ServiceTestpoxyService=(ServiceTest)JDKProxy.getPoxyObject(service);
ServiceTestpoxyService=(ServiceTest)CGLIBProxy.getPoxyObject(service);
System.out.println(poxyService.getClass().getSuperclass());
poxyService.saySomething("hello,MyQQcodeis107966750.");
poxyService.saySomething("what'syourname?");
poxyService.saySomething("onlyfortest,hehe.");
}
}

1, CGLIB實現方式是對代理的目標類進行繼承

2, 生成出了的代理類可以沒方法,生成出來的類可以直接轉換成目標類或目標類實現介面的實現類,因JAVA向上轉換

明顯特點:通過輸出看出,看出生成出的代理類的parent類為代理的目標類

4. Spring框架的動態代理是干什麼用的

spring的動態代理實現有兩種,一種第三方,一種是JDK的反射。
動態代理可以快速地實現工廠生成對象實例注入業務中使用到的實例。

5. spring 得不到動態代理類

把你的動態代理配置貼出來。

spring使用CGLIB強制代理,需要在配置文件頭部聲明

<!--聲明使用CGLIB自動創建代理Bean-->
<aop:aspectj-autoproxyproxy-target-class="true"/>

如果需要對業務層添加事物攔截還需要進行如下配置



<!--定義事務管理攔截器-->
<beanid="transactionInterceptor"
class="添加事物攔截處理類">
<!--注入DAL客戶端介面-->
<propertyname="添加數據源id"ref="dalClient"/>
</bean>
<!--定義bean自動代理容器-->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<propertyname="interceptorNames">
<list>
<!--配置攔截器-->
<value>transactionInterceptor</value>
</list>
</property>
<propertyname="beanNames">
<list>
<!--需要攔截serivce的beanName,支持通配-->
<value>*Service</value>
</list>
</property>

<propertyname="proxyTargetClass"value="true"/>

<propertyname="exposeProxy"value="true"/>
</bean>

6. 使用spring的動態代理功能時為什麼必須指定

攔截某些類的話,會判斷這個類是否實現了介面,如果實現了介面就會用jdk 動態代理來創建代理對象;如果沒有這個類沒有實現介面情況下,則用CGLIB來創建代理對象。
JDK動態代理:
public class JDKProxy implements InvocationHandler {
private Object targetObject;//代理的目標對象
public Object createProxyInstance(Object targetObject){
this.targetObject = targetObject;
/*
* 第一個參數設置代碼使用的類裝載器,一般採用跟目標類相同的類裝載器
* 第二個參數設置代理類實現的介面
* 第三個參數設置回調對象,當代理對象的方法被調用時,會委派給該參數指定對象的invoke方法
*/
return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),
this.targetObject.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(this.targetObject, args);//把方法調用委派給目標對象
}
}
當目標類實現了介面,我們可以使用jdk的Proxy來生成代理對象。
使用CGLIB生成代理:
public class CGLIBProxy implements MethodInterceptor {
private Object targetObject;//代理的目標對象
public Object createProxyInstance(Object targetObject){
this.targetObject = targetObject;
Enhancer enhancer = new Enhancer();//該類用於生成代理對象
enhancer.setSuperclass(this.targetObject.getClass());//設置父類
enhancer.setCallback(this);//設置回調用對象為本身
return enhancer.create();
}
public Object intercept(Object proxy, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
return methodProxy.invoke(this.targetObject, args);
}
}
CGLIB可以生成目標類的子類,並重寫父類非final修飾符的方法。

7. 關於spring aop的自動代理的問題

在配置文件中加入
<bean id="logBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<!--這個local是指作為攔截器的那個bean 。 根據你的需求寫-->
<ref local="logBefore"/>
</property>
<!-- 指定Controller類中的所有方法 -->
<property name="patterns">
<value>.*.*</value>
</property>
</bean>

8. spring中aop的動態代理機制有哪些

1.事務管理: (1)資料庫事務:(2)編程事務(3)聲明事物:Spring AOP-->聲明事物
2.日誌處理:
3.安全驗證: Spring AOP---OOP升級

靜態代理原理:目標對象:調用業務邏輯 代理對象:日誌管理
表示層調用--->代理對象(日誌管理)-->調用目標對象

動態代理原理:spring AOP採用動態代理來實現
(1)實現InvocationHandler介面
(2)創建代理類(通過java API)

9. 請問怎樣配置Spring的事務自動代理啊我配置的這個怎麼出現異常

<ref bean="transactionManger" /> 掉了一個a
transactionManager"

而且裡面也沒有聲明 transactionMangaer bean
其實這個配置很麻煩
為什麼不用Spring 2.0 的聲明式AOP

CGLIB代理在運行期間產生目標對象的子類,該子類通過裝飾器設計模式加入到Advice中。因為CGLIB代理是目標對象的子類,則必須考慮保證如下兩點:

目標類不能聲明成final,因為final類不能被繼承,無法生成代理。

目標方法也不能聲明成final,final方法不能被重寫,無法得到處理。