- package com.itheima.day03.aopframework;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import com.itheima.day03.Advice;
- public class BeanFactory {
- Properties prop = new Properties();
- public BeanFactory(InputStream is) {
- try {
- prop.load(is);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public Object getBean(String name) {
- // 获取类名
- String className = prop.getProperty(name);
- // System.out.println(className);
- Object bean = null;
- try {
- // 获取类字节码文件
- Class clazz = Class.forName(className);
- // 创建实例
- bean = clazz.newInstance();
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (InstantiationException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IllegalAccessException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if (bean instanceof ProxyFactoryBean) {
- Object proxy = null;
- ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean) bean;
- try {
- Advice advice = (Advice) Class.forName(
- prop.getProperty(name + ".advice")).newInstance();
- Object target = Class.forName(
- prop.getProperty(name + ".target")).newInstance();
- proxyFactoryBean.setAdvice(advice);
- proxyFactoryBean.setTarget(target);
- proxy = proxyFactoryBean.getProxy();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return proxy;
- }
- return bean;
- }
- }
复制代码- package com.itheima.day03.aopframework;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.Collection;
- import com.itheima.day03.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(),
- /* new Class[] { Collection.class }, */
- 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.afterMethod(method);
-
-
- return retVal;
- }
- });
- return proxy;
- }
- }
复制代码- package com.itheima.day03.aopframework;
- import java.io.InputStream;
- public class AopFrameWorkTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- InputStream is=AopFrameWorkTest.class.getResourceAsStream("config.properties");
- Object bean=new BeanFactory(is).getBean("xxx");
- System.out.println(bean.getClass().getName());
- }
- }
复制代码- package com.itheima.day03;
- import java.lang.reflect.Method;
- public interface Advice {
- public void beforeMethod(Method method);
-
- public void afterMethod(Method method);
- }
复制代码- #xxx=java.util.ArrayList
- xxx=com.itheima.day03.aopframework.ProxyFactoryBean
- xxx.advice=com.itheima.day03.MyAdvice
- xxx.target=java.util.ArrayList
复制代码
这个是关于动态代理的问题,求原理的解释。
很迷茫啊,这儿。完全没有接触过的东西。
有点迷糊。
求大神指点。
|