本帖最后由 孙百鑫 于 2013-5-15 21:01 编辑
- ************************配置文件****************************
- #xxx=java.util.ArrayList
- xxx=cn.itcast.day03.aopframework.ProxyFactoryBean
- xxx.advice=cn.itcast.day03.MyAdvice
- xxx.target=java.util.ArrayList
- ************************22222*****************************
- package cn.itcast.day03.aopframework;
- import java.io.InputStream;
- import java.util.Collection;
- public class AopFrameworkTest {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
-
- //用相对路径getResourceAsStream来加载配置文件
- InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties");
-
- //将ips流输出到BeanFactory类的构造方法中.并往getBean函数内传递XXX的键.
- Object bean = new BeanFactory(ips).getBean("xxx");
-
- //打印返回的值的名字.
- System.out.println(bean.getClass().getName());
- ((Collection)bean).clear();
- }
- }
- ********************333333333**********************************
- package cn.itcast.day03.aopframework;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import cn.itcast.day03.Advice;
- public class BeanFactory {
-
- //建立Properties对象用于接收传进来的流.
- Properties props = new Properties();
- public BeanFactory(InputStream ips){
- try {
- //将AopFrameworkTest流中的数据传入到Properties中
- props.load(ips);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- //将XXX的键传入到getBean中
- public Object getBean(String name){
-
- //获取XXX中的值
- String className = props.getProperty(name);
-
- //在try外边建立引用
- Object bean = null;
- try {
-
- //用Class.forName获取XXX中的字节码文件并用newInstance创建实例对象
- Class clazz = Class.forName(className);
- bean = clazz.newInstance();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- //对XXX中的值进行判断.是否实例化ProxyFactoryBean,如果不是直接返回到AopFrameworkTest并打印
- if(bean instanceof ProxyFactoryBean){
-
- //try外部建立引用
- Object proxy = null;
-
- //已知XXX的值肯定是ProxyFactoryBean的实力对象了.直接强转过来.
- ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
- try {
-
- //????????????????????????????????????????????????????没理解透这句话(props.getProperty(name + ".advice")).newInstance();
- //??????????????????????????????????????????????????????????????特别是name + ".advice"
- Advice advice = (Advice)Class.forName(props.getProperty(name + ".advice")).newInstance();
- Object target = Class.forName(props.getProperty(name + ".target")).newInstance();
-
- //这是proxyFactoryBean中的advice和target,
- proxyFactoryBean.setAdvice(advice);
- proxyFactoryBean.setTarget(target);
-
- //使用代理.
- proxy = proxyFactoryBean.getProxy();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //返回代理
- return proxy;
- }
- return bean;
- }
- }
- ****************************3333333333*****************************
- package cn.itcast.day03.aopframework;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import cn.itcast.day03.Advice;
- public class ProxyFactoryBean {
- //私有化
- private Advice advice;
- private Object target;
- public Object getProxy() {
- //使用Proxy代理类的静态方法newProxyInstance.并实现代理类的三个参数
- Object proxy3 = Proxy.newProxyInstance(
-
- //target的类加载器
- target.getClass().getClassLoader(),
-
- //new Class[]{Collection.class},
- //target的接口
- target.getClass().getInterfaces(),
-
- //InvocationHandler接口.实现内部类.
- new InvocationHandler(){
-
- //用内部类覆盖InvocationHandler中的方法proxy 代表对象, method,代表对象的方法, args代表参数
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- // TODO Auto-generated method stub
- // long beginTime = System.currentTimeMillis();
- // Object retVal = method.invoke(target, args);
- // long endTime = System.currentTimeMillis();
- // System.out.println("retVal run of " +(beginTime-endTime));
- // return retVal;
-
- //获取程序开始时间
- advice.beforeMethod(method);
-
- Object retVal = method.invoke(target, args);
-
- //获取结束时间
- advice.afterMethod(method);
-
- return retVal;
- }
- }
- );
- return proxy3;
- }
- //set和get方法
- public Advice getAdvice() {
- return advice;
- }
- public void setAdvice(Advice advice) {
- this.advice = advice;
- }
- public Object getTarget() {
- return target;
- }
- public void setTarget(Object target) {
- this.target = target;
- }
- }
- *************************************444444444***********************
- package cn.itcast.day03;
- import java.lang.reflect.Method;
- public class MyAdvice implements Advice {
- long beginTime =0;
- public void afterMethod(Method method) {
- // TODO Auto-generated method stub
- System.out.println("传智播客毕业了");
- long endTime = System.currentTimeMillis();
- System.out.println("retVal run of " +(beginTime-endTime));
-
- }
- public void beforeMethod(Method method){
- // TODO Auto-generated method stub
- System.out.println("传智播客开学了");
- beginTime = System.currentTimeMillis();
- }
- }
- *******************************5555555555********************************
- package cn.itcast.day03;
- import java.lang.reflect.Method;
- public interface Advice {
- void afterMethod(Method method);
- void beforeMethod(Method method);
- }
复制代码 |