This article deepens the understanding of dynamic proxy and AOP programming through a small framework for packaging and configuration of AOP functions implemented by dynamic proxy.
Create an object of the corresponding class based on the value (class full name) corresponding to the key xxx of the configuration file.
If and only if the value corresponding to xxx is com.iot.proxy.aopframework.ProxyFactoryBean , the corresponding dynamic proxy class object is generated. The target class and notification implementation class of the proxy object are configured by xxx.target and xxx.advice respectively.
config.propertiest is located in the aopframework package
#xxx=java.util.ArrayList
xxx=com.iot.proxy.aopframework.ProxyFactoryBean
xxx.advice=com.iot.proxy.MyAdvice
xxx.target=java.util.ArrayList
Package: com.iot.proxy.aopframework , with the following classes/interfaces:
package com.iot.proxy.aopframework;import java.lang.reflect.Method;/** * Created by brian on 2016/2/2. */public interface Advice { void beforeMethod(Method method); void afterMethod(Method method);} package com.iot.proxy.aopframework;import java.lang.reflect.Method;/** * Created by brian on 2016/2/2. */public class MyAdvice implements Advice{long beginTime = 0;@Override public void beforeMethod(Method method) {System.out.println(method.getName()+" before at "+beginTime);beginTime = System.currentTimeMillis();}@Override public void afterMethod(Method method) {long endTime = System.currentTimeMillis();System.out.println(method.getName()+" cost total "+ (endTime-beginTime));}} package com.iot.proxy.aopframework;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * Created by brian on 2016/2/2. */public class BeanFactory {Properties properties = new Properties();public BeanFactory(InputStream inputStream){try {properties.load(inputStream);}catch (IOException e) {e.printStackTrace();}}public Object getBean(String name){String className = properties.getProperty(name);Object bean = null;try {Class clazz = Class.forName(className);bean = clazz.newInstance();}catch (ClassNotFoundException e) {e.printStackTrace();}catch (InstantiationException e) {e.printStackTrace();}catch (IllegalAccessException e) {e.printStackTrace();}if (bean instanceof ProxyFactoryBean){ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;Advice advice = null;Object target = null;try {advice = (Advice) Class.forName(properties.getProperty(name+".advice")).newInstance();target = Class.forName(properties.getProperty(name+".target")).newInstance();}catch (InstantiationException e) {e.printStackTrace();}catch (IllegalAccessException e) {e.printStackTrace();}catch (ClassNotFoundException e) {e.printStackTrace();}proxyFactoryBean.setAdvice(advice);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;/** * Created by brian on 2016/2/3. */public class ProxyFactoryBean {private Object target;private Advice advice;public Object getProxy(){Object proxy = Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {advice.beforeMethod(method);Object retVal = method.invoke(target,args);advice.aftereMethod(method);return retVal;}});return proxy;}public Object getTarget() {return target;}public void setTarget(Object target) {this.target = target;}public Advice getAdvice() {return advice;}public void setAdvice(Advice advice) {this.advice = advice;}} package com.iot.proxy.aopframework;import java.io.InputStream;import java.util.Collection;/** * Created by brian on 2016/2/3. */public class AopFrameWorkTest {public static void main(String[] args) {InputStream inputStream = AopFrameWorkTest.class.getResourceAsStream("config.properties");Object bean = new BeanFactory(inputStream).getBean("xxx");System.out.println(bean.getClass().getName());((Collection) bean).clear();}}The output is:
com.sun.proxy.$Proxy0
clear before at 0
clear cost total 0
The output is:
java.util.ArrayList
It can be seen that changing the configuration file can change the running results of the code, thereby achieving flexible results.
The above is the entire content of this article about the small framework instance code for the packaging and configuration of Java's AOP functions. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!