Java 動態代理模仿Spring AOP

來源:互聯網
上載者:User
Advice切面介面

package aop;import java.lang.reflect.Method;public interface Advice {void beforeMethed(Method method);void afterMethed(Method method);}
MyAdvice切面實作類別

package aop;import java.lang.reflect.Method;public class MyAdvice implements Advice {private long beginTime=0;@Overridepublic void beforeMethed(Method method) {beginTime=System.currentTimeMillis();System.out.println("before method:"+method.getName());}@Overridepublic void afterMethed(Method method) {long endTime=System.currentTimeMillis();System.out.println("end method:"+method.getName()+" method use "+String.valueOf(endTime-beginTime)+" millisecond");}}
代理工廠

package aop;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ProxFactoryBean {private Object target;private Advice advice;public Object getProx(){Object prox= Proxy.newProxyInstance(target.getClass().getClassLoader(),//類載入器 target.getClass().getInterfaces(), //目標對象的介面new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {advice.beforeMethed(method);//調用目標之前Object resVal= method.invoke(target, args);//調用目標advice.afterMethed(method);//調用目標之後return resVal;}});return prox;//這裡放回的是目標對象的介面,所以調用的時候也要用介面}public void setTarget(Object target) {this.target = target;}public void setAdvice(Advice advice) {this.advice = advice;}}

Bean工廠

package aop;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class BeanFactory {private Properties properties=new Properties();public BeanFactory(InputStream inStream) throws IOException {properties.load(inStream);}public Object getBean(String beanName) {String className=properties.getProperty(beanName);//到設定檔中尋找bean的全路徑Object bean=null;try {bean = Class.forName(className).newInstance();//執行個體化Bean} catch (Exception e) {e.printStackTrace();} if(bean instanceof ProxFactoryBean){//如果Bean是代理對象ProxFactoryBean proxFactoryBean=(ProxFactoryBean)bean;//擷取代理對象try {Advice advice = (Advice) Class.forName(properties.getProperty(beanName +".advice")).newInstance();Object target=Class.forName(properties.getProperty(beanName +".target")).newInstance();proxFactoryBean.setAdvice(advice);//設定切面proxFactoryBean.setTarget(target);//設定代理那個對象} catch (Exception e) {e.printStackTrace();}return proxFactoryBean.getProx();}return bean;}}

設定檔

#xxx=java.util.ArrayListxxx=aop.ProxFactoryBeanxxx.advice=aop.MyAdvicexxx.target=java.util.ArrayList
測試類別

package aop;import java.io.IOException;import java.util.ArrayList;import java.util.Collection;public class AopMain {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {BeanFactory factory=new BeanFactory(AopMain.class.getResourceAsStream("bean.properties"));Collection list=(Collection) factory.getBean("xxx");//這裡要用介面list.add("a");System.out.println(list.size());}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.