이 기사는 동적 프록시에서 구현 한 AOP 기능의 포장 및 구성을위한 작은 프레임 워크를 통해 동적 프록시 및 AOP 프로그래밍에 대한 이해를 심화시킵니다.
구성 파일의 키 xxx에 해당하는 값 (클래스 전체 이름)에 따라 해당 클래스의 객체를 만듭니다.
xxx에 해당하는 값이 com.iot.proxy.aopframework.ProxyFactoryBean 인 경우에만 해당 동적 프록시 클래스 객체가 생성됩니다. 프록시 오브젝트의 대상 클래스 및 알림 구현 클래스는 각각 xxx.target 및 xxx.advice 로 구성됩니다.
config.propertiest aopframework 패키지에 있습니다
#xxx = java.util.arraylist
xxx = com.iot.proxy.aopframework.proxyfactorybean
xxx.advice = com.iot.proxy.myadvice
xxx.target = java.util.arraylist
패키지 : com.iot.proxy.aopframework , 다음 클래스/인터페이스가 포함되어 있습니다.
package com.iot.proxy.aopframework; import java.lang.reflect.method;/*** Brian이 2016/2/2에 작성했습니다. */공개 인터페이스 조언 {void beforemethod (메소드 메소드); void AfterMethod (메소드 메소드);} package com.iot.proxy.aopframework; import java.lang.reflect.method;/*** Brian이 2016/2/2에 작성했습니다. */public class myAdvice는 조언을 구현 {long begintime = 0;@Override public void beforemethod (메소드 메소드) {system.out.println ( "+begintime); begintime = system.currentTimeMillis ()@}@atedride public void AfterMedod (method method) I System.CurrentTimeMillis (); System.out.println (Method.getName ()+ "총"+ (EndTime-BeginTime));}} package com.iot.proxy.aopframework; import java.io.ioexception; import java.io.inputstream; import java.util.properties;/*** Brian이 2016/2/2에 생성했습니다. */public class beanfactory {properties properties = new Properties (); public beanFactory (inputStream inputStream) {try {properties.load (inputStream);} catch (ioException e) {e.printstackTrace ();}} public 객체 getBean (string name = null); {class clazz = class.forname (className); bean = clazz.newinstance ();} catch (classNotFoundException e) {e.printstackTrace ();} catch (instantiationException e) {e.printstacktrace ();} catch (e.premalaccessexception e) {e.premtrace (); proxyFactoryBean) {proxyFactoryBean proxyFactoryBean = (proxyFactoryBean) bean; 조언 조언 = null; 객체 target = null; try {joction = (조언) class.forname (properties.getProperty (name+". 조언"); target = class.forname (properties.getProperty (name+". target")). newInstance ();} catch (InstantiationException e) {e.printstacktrace ();} catch (불법 행사 exception e) {e.printstacktrace ();} catch (classnotfoundExcept e) {e.printstacktrace ();} proxyFactoryBean.setAdvice (조언); proxyFactoryBean.setTarget (target); Object proxy = ((proxyFactoryBean) bean) .getProxy (); return proxy;} return bean;}} package com.iot.proxy.aopframework; import java.lang.reflect.invocationhandler; import java.lang.reflect.method; import java.lang.reflect.proxy;/*** 2016/2/3에 Brian이 생성했습니다. */public class proxyFactoryBean {개인 대상 대상; 개인 조언 조언; public object getProxy () {Object Proxy = proxy.newProxyInstance (target.getClass (). getClassLoader (), target.getClass (). getInterfaces (), New invociationHandler () @OverRide invoke (대상) {Advice.beforemethod (method); object retval = method.invoke (target, args); adceptremethod (method); return retval;}}; return proxy;} public object getTarget () {retep target;} public void settarget (object target) {target =} public reflice (target) getAdvice () setAdvice (조언 조언) {this.advice = 조언;}} package com.iot.proxy.aopframework; import java.io.inputStream; import java.util.collection;/*** Brian이 2016/2/3에 작성했습니다. */public class aopframeworkTest {public static void main (string [] args) {inputStream inputStream = aopframeworkTest.class.getResourceasStream ( "config.properties"); 객체 bean = new beanFactory (inputStream) .getBean ( "xxx"); System.out.println (bean.getClass (). getName ()); ((Collection) Bean) .clear ();}}출력은 다음과 같습니다.
com.sun.proxy. $ proxy0
0시에 지우십시오
명확한 비용 총 0
출력은 다음과 같습니다.
java.util.arraylist
구성 파일을 변경하면 코드의 실행중인 결과가 변경되어 유연한 결과를 얻을 수 있습니다.
위는 Java의 AOP 함수의 포장 및 구성을위한 작은 프레임 워크 인스턴스 코드에 대한이 기사의 전체 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!