首页 > 代码库 > 黑马程序员——高新技术

黑马程序员——高新技术

 

实现类似spring的可配置的AOP框架

从源头上说 aop总共有那么几种方法
1.用AspectJ,这需要引入该语法的编译器,但是功能最强大,spring貌似有对其提供支持或者封装,用spring可以不用引入AspectJ编译器直接是用Annotation来是用aspectJ的aop功能。

2.是用asm或apache commons项目的BCEL包直接操作class文件,如。spring中需要引入asm包,所以里面肯定有引用该功能的地方。如Hibernate即是使用该包实现实体映射

工厂类BeanFactory负责创建目标类或代理类的实例对象,并通过配置文件实现切换。其getBean方法根据参数字符串返回一个相应的实例对象,如果参数字符串在配置文件中对应的类名不是ProxyFactoryBean,则直接返回该类的实例对象,否则,返回该类实例对象的getProxy方法返回的对象。


BeanFactory的构造方法接收代表配置文件的输入流对象,配置文件格式如下:
#xxx=java.util.ArrayList
xxx=cn.itcast.ProxyFactoryBean
xxx.target=java.util.ArrayList
xxx.advice=cn.itcast.MyAdvice

ProxyFacotryBean充当封装生成动态代理的工厂,需要为工厂类提供哪些配置参数信息?
目标
通知


编写客户端应用:
编写实现Advice接口的类和在配置文件中进行配置
调用BeanFactory获取对象

 

示例:

ProxyFactoryBean.java

package com.itheima.day3.aopframework;

 

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

 

import com.itheima.day3.Advice;

 

public class ProxyFactoryBean {

       private Advice advice ;

       private Object target ;

      

       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;

      }

 

       public Object getProxy(){

            Object proxy = Proxy. newProxyInstance(

                target .getClass().getClassLoader(),

                target .getClass().getInterfaces(),

                new InvocationHandler() {

                      public Object invoke(Object proxy, Method method, Object[] args)

                                  throws Throwable {

                         advice .beforeAdvice(method);

                         Object retVal = method.invoke( target ,args);

                         advice .afterAdvice(method);

                         return retVal;

                     }

               }

         );

         return proxy;

   };

}

 

BeanFactory.java

package com.itheima.day3.aopframework;

 

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

 

import com.itheima.day3.Advice;

 

publicclassBeanFactory {

 

      Propertiesprops=newProperties();

      

      publicBeanFactory(InputStream ips){

            try{

                  props.load(ips);

            }catch(IOException e) {

                  e.printStackTrace();

            }

      }

      

      publicObject getBean(String name) {

            String className =props.getProperty(name);

            Object bean =null;

            try{

                  Class clazz = Class.forName(className);

                  bean = clazz.newInstance();

            }catch(Exception e) {

                  e.printStackTrace();

            }

            if(beaninstanceofProxyFactoryBean){

                  ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;

                  Object proxy =null;

                  try{

                        Advice advice = (Advice) Class. forName(

                                    props.getProperty(name +".advice")).newInstance();

                        Object target = Class. forName(

                                    props.getProperty(name +".target")).newInstance();

                        proxyFactoryBean.setAdvice(advice);

                        proxyFactoryBean.setTarget(target);

                        proxy = proxyFactoryBean.getProxy();

                  }catch(Exception e) {

                        e.printStackTrace();

                  }

                  returnproxy;

            }

            returnbean;

      }

}

 

AopFrameworkTest.java

package com.itheima.day3.aopframework;

 

import java.io.InputStream;

 

publicclassAopFrameworkTest {

 

      publicstaticvoidmain(String[] args)throwsException{

            InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties");

            Object bean =newBeanFactory(ips).getBean("xxx");

            System.out.println(bean.getClass().getName());

      }

}

 
 

详情请查看:http://edu.csdn.net/heima

黑马程序员——高新技术