锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久一区二区三区av,视频福利在线,999久久精品http://www.aygfsteel.com/yuyee/category/46754.htmlzh-cnWed, 03 Nov 2010 20:53:54 GMTWed, 03 Nov 2010 20:53:54 GMT60cglib鍏ラ棬http://www.aygfsteel.com/yuyee/archive/2010/10/26/336181.html緹旂緤緹旂緤Tue, 26 Oct 2010 06:27:00 GMThttp://www.aygfsteel.com/yuyee/archive/2010/10/26/336181.htmlhttp://www.aygfsteel.com/yuyee/comments/336181.htmlhttp://www.aygfsteel.com/yuyee/archive/2010/10/26/336181.html#Feedback0http://www.aygfsteel.com/yuyee/comments/commentRss/336181.htmlhttp://www.aygfsteel.com/yuyee/services/trackbacks/336181.htmlCGLIB鏄竴涓己澶х殑浠g爜鐢熸垚鍖咃紝鍦ˋSM涓婂緩绔嬶紝鍦ㄤ竴浜涘紑婧愬伐鍏蜂腑緇忓父鍙互鐪嬪埌浠栫殑韜獎錛屾瘮濡俬ibernate,spring銆?/div>
CGLIB搴曞眰閫氳繃瀛楄妭鐮佸鐞嗘鏋禔SM鏉ュ皢瀛楄妭鐮佺敓鎴愭柊鐨勭被錛屽湪spring AOP涓笉寮哄埗浣跨敤CGLIB錛岄粯璁ゆ槸JDK鍔ㄦ佷唬鐞嗐?/div>
CGLIB 鍖呮儏鍐碉細
net.sf.cglib.core:搴曞眰瀛楄妭鐮佸鐞嗙被錛屽ぇ閮ㄥ垎涓嶢SM鏈夊叧銆?/div>
net.sf.cglib.transform:緙栬瘧鏈熸垨榪愯鏈熷拰綾繪枃浠剁殑杞崲
net.sf.cglib.proxy錛氬疄鐜板垱寤轟唬鐞嗗拰鏂規硶鎷︽埅鍣ㄧ殑綾?/div>
net.sf.cglib.reflect:瀹炵幇蹇熸斁灝?/div>
net.sf.cglib.util:宸ュ叿鍖?/div>
net.sf.cglib.beans:javabean鐩稿叧宸ュ叿綾?/div>
閫氳繃CGLIB鍒涘緩鍔ㄦ佷唬鐞嗭紝鏈川涓婏紝浠栨槸鍔ㄦ佺殑鐢熸垚鐩爣綾葷殑瀛愮被錛岃鐩栫洰鏍囩被鎵鏈変笉鏄痜inal鐨勬柟娉曪紝騫剁粰浠栦滑璁劇疆濂絚allback,鍥犳錛屽師鏈夌被鐨勬瘡涓柟娉曡皟鐢ㄥ氨浼氬彉鎴愯嚜瀹氫箟鐨勬嫤鎴柟娉曘?/div>
鍒涘緩鍔ㄦ佷唬鐞嗘椂閫氬父瑕佺敤鍒板涓媋pi:net.sf.cglib.proxy.Callback榪欎釜鎺ュ彛錛屼粬鏄緢鍏抽敭鐨勪竴涓帴鍙o紝鎵鏈夎net.sf.cglib.proxy.Enhancer綾昏皟鐢ㄧ殑鍥炶皟鍊熷彛閮借緇ф壙榪欎釜鎺ュ彛
濡傦細public interface MethodInterceptor
extends Callback
{
    /**
     * All generated proxied methods call this method instead of the original method.
     * The original method may either be invoked by normal reflection using the Method object,
     * or by using the MethodProxy (faster).
     * @param obj "this", the enhanced object
     * @param method intercepted Method
     * @param args argument array; primitive types are wrapped
     * @param proxy used to invoke super (non-intercepted method); may be called
     * as many times as needed
     * @throws Throwable any exception may be thrown; if so, super method will not be invoked
     * @return any value compatible with the signature of the proxied method. Method returning void will ignore this value.
     * @see MethodProxy
     */    
    public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,
                               MethodProxy proxy) throws Throwable;

}
榪欎釜鏄熀涓庢柟娉曠殑鍥炶皟錛岀涓涓弬鏁版槸浠g悊瀵硅薄錛岀浜屼釜鏄鎷︽埅鐨勬柟娉曞璞★紝絎笁涓槸鏂規硶鍙傛暟錛岀鍥涗釜鏄柟娉曠殑浠g悊瀵硅薄銆?/div>
public class MyClass {
public void method1() {
System.out.println("method1");
}

public void method2() {
System.out.println("method2");
}
}
鎷︽埅鍣細
public class MethodInterceptImpl implements MethodInterceptor {

public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
System.out.println(arg1.getName()+"--intercept");
arg3.invokeSuper(arg0, arg2);// 榪欓噷鍏跺疄涔熷彲浠ョ敤鍘熸潵鐨勬柟娉曞璞℃潵鎵ц錛屼絾鏄ц兘涓婁笉濡俢glib鐨勬柟娉曚唬鐞嗙被
return null;
}
public class MainTest {
public static void main(String[] args) {
simpleTest();
}

private static void simpleTest() {
Enhancer en = new Enhancer();
en.setCallback(new MethodInterceptImpl());
en.setSuperclass(MyClass.class);
MyClass m = (MyClass) en.create();
m.method1();
m.method2();
}
緇撴灉錛歮ethod1--intercept
method1
method2--intercept
method2
鐜板疄欏圭洰涓彲鑳藉瓨鍦ㄦ煇浜涢渶姹傦紝姣斿method1闇瑕佹嫤鎴紝鑰宮ethod2涓嶉渶瑕佹嫤鎴傞偅鎴戜滑鍙互瀵筩allback鍋氫笅閫夋嫨,浣跨敤net.sf.cglib.proxy.CallbackFilter鍋氫竴浜涜繃婊ゃ?/div>
public class MethodFilterImpl implements CallbackFilter {

private final static int execute = 0;
private final static int unexecute = 1;

public int accept(Method method) {
String methodName = method.getName();
if("method1".equals(methodName)){
return execute;
}
return unexecute;
}

}

璋冪敤鐨勬椂鍊欓渶瑕佺粰callback璁劇疆濂界儲寮曚綅緗紝鍥犱負accept鐨勮繑鍥炲煎氨鏄痗allbacks鏁扮粍鐨勭儲寮曚綅緗?/div>
private static void filterTest() {
Enhancer en = new Enhancer();
Callback[] callbacks = new Callback[] { new MethodInterceptImpl(),
NoOp.INSTANCE };//榪欓噷鎷︽埅鍣ㄧ殑绱㈠紩浣嶇疆瑕佷笌filter閲岀殑璁劇疆涓鑷?/div>
en.setCallbacks(callbacks);
en.setSuperclass(MyClass.class);
en.setCallbackFilter(new MethodFilterImpl());
MyClass m = (MyClass) en.create();
m.method1();
m.method2();
}
榪欓噷callbacks[1]榪欎釜浣嶇疆浣跨敤浜哊oOp榪欎釜鍥炶皟鎺ュ彛
public interface NoOp extends Callback
{
    /**
     * A thread-safe singleton instance of the <code>NoOp</code> callback.
     */
    public static final NoOp INSTANCE = new NoOp() { };
}
榪欎釜callback鍏跺疄灝辨槸鐩存帴鎶婁換鍔″媧劇粰榪欎釜鏂規硶鍦ㄧ埗綾諱腑鐨勫疄鐜幫紝鍏跺疄涔熺瓑鍚屼簬娌″仛浠涔堥澶栫殑浜嬫儏
鎵ц緇撴灉錛歮ethod1--intercept
method1
method2
method2 騫舵湭琚玀ethodInterceptImpl鎷︽埅

}


緹旂緤 2010-10-26 14:27 鍙戣〃璇勮
]]>浣跨敤Annotation鏉ュ畬鎴恠pring aophttp://www.aygfsteel.com/yuyee/archive/2010/10/25/336106.html緹旂緤緹旂緤Mon, 25 Oct 2010 08:52:00 GMThttp://www.aygfsteel.com/yuyee/archive/2010/10/25/336106.html浣跨敤annotation鏉ヨ〃紺簆ointcut錛岄渶瑕佸湪xml閲岄厤緗畂rg.springframework.aop.support.annotation.AnnotationMatchingPointcut錛?/div>
濡傦細<bean name="timeoutPointcut"
class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut">
<constructor-arg index="0"
value="com.hengtian.fxfundsystem.aop.annotation.UseAOPAnnotation">
</constructor-arg>
<constructor-arg index="1"
value="com.hengtian.fxfundsystem.aop.annotation.TimeoutAnnotation" />
</bean>
絎竴涓弬鏁版槸鏍囧湪鐩爣綾諱笂鐨刟nnotation,絎簩澶╁弬鏁版槸鏍囨敞鍦ㄦ柟娉曚笂鐨凙nnotation
鍥犳錛岄鍏堣鍦ㄨ嚜宸辯殑宸ョ▼閲屽畾涔?涓猘nnotation錛屼竴涓負TimeoutAnnotation錛屼竴涓負UseAOPAnnotation
@Target({ElementType.TYPE })//琛ㄧず綾繪爣娉?/div>
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface UseAOPAnnotation {

}
@Target({ElementType.METHOD})//琛ㄧず鏂規硶鏍囨敞
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeoutAnnotation {
/**
* 榛樿鍊?-1)錛氳〃紺轟笉鍚敤瓚呮椂
*/
public static final long DefaultValue = -1;

/**
* 瓚呮椂鏃墮棿錛屽崟浣嶆槸ms
* @return
*/
long value() default DefaultValue;

}
鍦ㄩ渶瑕丄OP鐨勭被涓婁嬌鐢?/div>
@UseAOPAnnotation
@Component("StrongDuck")  
public class StrongDuck {
@TimeoutAnnotation(70000)
public void say(){
System.out.println("I am a strong duck");
}
}
timeout閫氱煡錛?/div>
public class TimeoutAdvice implements MethodInterceptor {

/*
* (non-Javadoc)
* @see
* org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept
* .MethodInvocation)
*/
public Object invoke(final MethodInvocation methodInvocation)
throws Throwable {

Method targetMethod = methodInvocation.getMethod();
TimeoutAnnotation timeoutAnnotation = targetMethod
.getAnnotation(TimeoutAnnotation.class);
if (timeoutAnnotation != null) {
long waitTime = timeoutAnnotation.value();
if (waitTime != TimeoutAnnotation.DefaultValue) {
Future<?> task = ThreadManager.ExecutorService
.submit(encapsulateSyncToAsync(methodInvocation));
return getAsyncResult(waitTime, task);
}

}
return methodInvocation.proceed();
}
XML涓厤緗嫤鎴櫒錛?/div>
<bean id="timeoutInterceptor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="timeoutAdvice" />
<property name="pointcut" ref="timeoutPointcut" />
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="proxyTargetClass"><!-- 鍩轟簬綾葷殑浠g悊 -->
<value>true</value>
</property>
</bean>
榪欐牱錛宻pring灝變細涓轟綘鐢熸垚aop浠g悊綾誨疄鐜癆OP




緹旂緤 2010-10-25 16:52 鍙戣〃璇勮
]]>JDK浠g悊http://www.aygfsteel.com/yuyee/archive/2010/10/25/336046.html緹旂緤緹旂緤Sun, 24 Oct 2010 16:20:00 GMThttp://www.aygfsteel.com/yuyee/archive/2010/10/25/336046.html
/**
 * 琛屼負鎺ュ彛
 * 
 * @author Administrator
 * 
 */
public interface Study {
public void doStudy();
}
/**
 * 鐩爣瀵硅薄
 * 
 * @author Administrator
 * 
 */
public class StudyImpl implements Study {

@Override
public void doStudy() {
System.out.println("study jdk proxy");
}

}


/**
 * 浠g悊綾?/div>
 * @author Administrator
 *
 */
public class JDKProxy implements InvocationHandler {

private Study s;

public JDKProxy(Study s) {
this.s = s;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("before jdkproxy");
method.invoke(s, args);
System.out.println("after jdkproxy");
return null;
}
}

搴旂敤涓繘琛岃皟鐢細
public class Test {
public static void main(String[] args) throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
Class clazz = StudyImpl.class;//鑾峰彇鐩爣綾葷殑Class瀵硅薄
Study st = new StudyImpl();//瀹炰緥鍖栦竴涓洰鏍囩被
Class proxClass = Proxy.getProxyClass(clazz.getClassLoader(), clazz
.getInterfaces());//閫氳繃Proxy闈欐佹柟娉曪紝鑾峰彇涓涓簬鐩爣綾誨疄鐜板悓鏍鋒帴鍙g殑Class瀵硅薄錛屼篃鍙互璇存槸鍏勫紵綾?/div>
Constructor c = proxClass
.getConstructor(new Class[] { InvocationHandler.class });//緇欒繖涓被涓涓寚瀹氱殑鍏紑鐨勬瀯閫犳柟娉?/div>
Study ds = (Study) c.newInstance(new Object[] { new JDKProxy(st) });//瀹炰緥鍖栦竴涓唬鐞嗙被
}
}


緹旂緤 2010-10-25 00:20 鍙戣〃璇勮
]]>AOP鏃ヨhttp://www.aygfsteel.com/yuyee/archive/2010/10/24/336045.html緹旂緤緹旂緤Sun, 24 Oct 2010 15:48:00 GMThttp://www.aygfsteel.com/yuyee/archive/2010/10/24/336045.html
  鏈榪戝湪瀛︾潃鎬葷粨錛屽洜涓哄父鎰熻鍋氬畬涓滆タ錛岄暱涔呬笉鐢紝榪囨鏃墮棿灝卞繕錛屼笅嬈℃兂澶嶄範涓嬮兘涓嶈銆?/div>
 AOP錛岄潰鍚戞柟闈㈢紪紼嬶紝鎴戜滑緋葷粺涓湁寰堝緇勪歡緇勬垚錛屾瘡涓涓粍浠惰礋璐d竴閮ㄥ垎涓氬姟鍔熻兘,
 浣嗘槸榪欎簺緇勪歡寰寰鍙堝甫鏈変竴閮ㄥ垎鏍稿績鍔熻兘澶栫殑闄勫姞鍔熻兘錛岃屼笖鏄噸澶嶄唬鐮侊紝濡傦細鏃ュ織錛屽畨鍏紝浜嬪姟錛?閫氳繃AOP錛屽疄鐜拌繖浜涢檮鍔犲姛鑳界殑鍙噸澶嶅埄鐢ㄣ?/div>
 
 AOP鍏跺疄鍙互鐪嬩綔鏄唬鐞嗘ā寮忕殑鎵╁睍,鍐欎竴涓畝鍗曠殑浠g悊
 
public class ProxySubject implements ProxyInterface {
private Subject innerObj;// 鐪熸鐨勫璞?/div>

@Override
public void handle() {
innerObj = new Subject();
// 鍋氶澶栫殑浜嬫儏
innerObj.handle();// 鐪熸鐨勫鐞嗕笟鍔?/div>
// 鍋氶澶栫殑浜嬫儏
}
}

 Aop涓彨浠g悊綾諱負advice,鍙互鏈夊涓繛鎺ヨ搗鏉ワ紝涓涓簨浠墮摼

  瀹炵幇AOP錛岃繕闇瑕佹秹鍙婂埌JAVA鐨勫弽灝勶紝閫氳繃鍙嶅皠錛岃幏鍙杕ethod瀵硅薄錛屽茍鎵ц鏂規硶錛孉OP瑙勮寖鍖呴噷鏈変釜鎺ュ彛

 

 

public interface MethodInterceptor extends Interceptor {

    

     *Implement this method to perform extra treatments before and

     * after the invocation. Polite implementations would certainly

     * like to invoke {@link Joinpoint#proceed()}.

     *

     * @param invocation the method invocation joinpoint

     * @return the result of the call to {@link

     * Joinpoint#proceed()}, might be intercepted by the

     * interceptor.

     *

     * @throws Throwable if the interceptors or the

     * target-object throws an exception.  

     Object invoke(MethodInvocation invocation) throws Throwable;

      锝?/p>

      閫氳繃 MethodInvocation鐨?getMethod()鑾峰緱鏂規硶瀵硅薄錛屽茍鎵ц

      榪欐牱錛屽鏋滀綘涓涓被涓湁寰堝鏂規硶錛屽氨鍙互閫氳繃榪欎箞涓涓?/p>

      

      AOP涓敓鎴愪唬鐞嗙被鏈?縐嶆柟寮忥紝

      1.闈欐佺紪璇戞椂鏈燂紝婧愪唬鐮佺敓鎴愶紝涓烘瘡涓鍚堟潯浠剁殑綾葷敓鎴愪竴涓唬鐞嗙被

      2.闈欐佸瓧鑺傜爜澧炲己閫氳繃ASM/CGLIB鍒嗘瀽瀛楄妭鐮侊紝鐢熸垚浠g悊綾?/p>

      3.鍔ㄦ佸瓧鑺傜爜澧炲己錛宻pring aop灝辨槸榪欐牱,ASM/CGLIB鎴栬匤DK鐨勫姩鎬佷唬鐞?/p>

      AOP鐨勫師鐞嗗彲浠ョ悊瑙d負浠g悊妯″紡+鏀懼皠+鑷姩瀛楄妭鐮佺敓鎴?/p>

Spring涓姩鎬佷唬鐞嗘湁2縐嶆柟寮忥紝涓縐嶆槸閫氳繃CGLIB錛屼竴縐嶆槸JDK銆?/p>

浠涔堟椂鍊欓噰鐢↗DK浠涔堟椂鍊欓噰鐢–GLIB鍔ㄦ佺敓鎴愶紝涓昏鍙栧喅浜庝綘鍙堟病瀹炵幇鎺ュ彛錛孞DK鍔ㄦ佷唬鐞嗛渶瑕佺洰鏍囩被瀹炵幇鏌愭帴鍙o紝鑰孋GLIB鍒欐槸鐢熸垚鐩爣綾葷殑瀛愮被

public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {

if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {

Class targetClass = config.getTargetClass();

if (targetClass == null) {

throw new AopConfigException("TargetSource cannot determine target class: " +

"Either an interface or a target is required for proxy creation.");

}

if (targetClass.isInterface()) {

return new JdkDynamicAopProxy(config);

}

if (!cglibAvailable) {

throw new AopConfigException(

"Cannot proxy target class because CGLIB2 is not available. " +

"Add CGLIB to the class path or specify proxy interfaces.");

}

return CglibProxyFactory.createCglibProxy(config);

}

else {

return new JdkDynamicAopProxy(config);

}

}

榪欎釜鏄疍efaultAopProxyFactory閲屽垱寤轟唬鐞嗙被鐨勬柟娉?/p>


 

 



緹旂緤 2010-10-24 23:48 鍙戣〃璇勮
]]> 主站蜘蛛池模板: 甘南县| 天长市| 绵阳市| 通化市| 专栏| 娱乐| 讷河市| 泽库县| 沁阳市| 宜川县| 宣武区| 株洲县| 福泉市| 当雄县| 长顺县| 师宗县| 高要市| 凌云县| 贡嘎县| 沙坪坝区| 定安县| 六安市| 汤原县| 竹北市| 宝坻区| 芒康县| 乐至县| 杨浦区| 澎湖县| 吉安县| 尉犁县| 陇川县| 修武县| 循化| 广东省| 绥棱县| 绩溪县| 舞钢市| 蓬溪县| 九龙城区| 霍州市|