1, the core business of interface and implementation
public interface IManager { void add(String item); }
public class IManagerImpl implements IManager { private List<String> list = new ArrayList<>(); @Override public void add(String item) { list.add(item); } }
2, notification of interface and implementation
public interface Advice { void beforeAdvice(); void afterAdvice(); }
public class AdviceImpl implements Advice { @Override public void beforeAdvice() { System.out.println("Method start time: " + System.currentTimeMillis()); } @Override public void afterAdvice() { System.out.println("Method end time: " + System.currentTimeMillis()); } }
3, the dynamic proxy class (associated with the core business of the notification section)
public class the ProxyFactory the implements of InvocationHandler { // the proxy object Private Object target; // notify Private the Advice the advice; / ** * returns the proxy object moving target object * @return * / public Object The getProxy () { Object Proxy = the Proxy.newProxyInstance (. target.getClass () getClassLoader (), target.getClass () The getInterfaces (),. the this ); return Proxy; } @Override public Object Invoke (Object Proxy, Method, Method, Object [] args)throws Throwable { advice.beforeAdvice(); Object obj = method.invoke(target, args); advice.afterAdvice(); return obj; } 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; } }
4, reflecting achieve agency, business, inform through the configuration file
public class the BeanFactory { the Properties prop = new new the Properties (); public the BeanFactory (the InputStream INPUT) { the try { prop.load (INPUT); } the catch (IOException E) { e.printStackTrace (); } } / ** * Get the Proxy ( Object agent) examples * @param name * @return * / public Object the getBean (String name) { // Get the class name ProxyFactory String className = prop.getProperty (name + ".proxy" ); Proxy Object = null ; the try { // Get the class object ProxyFactory Class = proxyClass the Class.forName (className); Proxy = proxyClass.newInstance (); // instantiate the target object based on the advice profile Object target = Class.forName ( prop.getProperty (name + ".target" )) the newInstance ();. Object the advice = the Class.forName (prop.getProperty (name + ".advice" ).) the newInstance (); // achieved by introspection ProxyFactory attribute assignment the BeanInfo BeanInfo = Introspector.getBeanInfo (proxyClass); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : propertyDescriptors) { String propertyName = pd.getName(); Method writeMethod = pd.getWriteMethod(); if ("target".equals(propertyName)) { writeMethod.invoke(proxy, target); } else if ("advice".equals(propertyName)) { writeMethod.invoke(proxy, advice); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IntrospectionException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return proxy; } }
5, test
public class AopTest { public static void main (String [] args) { // read the configuration file the InputStream INPUT = Thread.currentThread () getContextClassLoader (). .getResourceAsStream ( "CN / latiny / AOPA / bean.properties" ); / / create a factory class bean BeanFactory beanFactory = new new BeanFactory (the INPUT); // obtain the proxy object the ProxyFactory the ProxyFactory = (the ProxyFactory) BeanFactory.getBean ( "bean" ); // call through the proxy object iManager bean = (iManager) proxyFactory.getProxy (); bean.add ( "Latiny"); } }