BeanFactory和FactoryBean的区别
1、BeanFactory是spring IOC容器的顶层接口,定义了IOC容器的通用方法和规范。
2、FactoryBean首先是一个bean,但它又是一个能产生bean的工厂,主要作用就是提供了一种灵活创建bean的方式,让用户可以定制实例化bean的逻辑。
获取普通bean,getBean(beanName)
获取工厂bean,getBean("&"+beanName),前面加&。
比如AOP中的ProxyFactoryBean,模拟代理对象的逻辑:
/** * my factory bean<p> * 代理一个类,拦截该类的所有方法,在方法的调用前后进行日志的输出 * @author daniel.zhao * */ public class MyFactoryBean implements FactoryBean<Object>, InitializingBean, DisposableBean { private static final Logger logger = LoggerFactory.getLogger(MyFactoryBean.class); private String interfaceName; private Object target; private Object proxyObj; @Override public void destroy() throws Exception { logger.debug("destroy......"); } @Override public void afterPropertiesSet() throws Exception { proxyObj = Proxy.newProxyInstance( this.getClass().getClassLoader(), new Class[] { Class.forName(interfaceName) }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { logger.debug("invoke method......" + method.getName()); logger.debug("invoke method before......" + System.currentTimeMillis()); Object result = method.invoke(target, args); logger.debug("invoke method after......" + System.currentTimeMillis()); return result; } }); logger.debug("afterPropertiesSet......"); } @Override public Object getObject() throws Exception { logger.debug("getObject......"); return proxyObj; } @Override public Class<?> getObjectType() { return proxyObj == null ? Object.class : proxyObj.getClass(); } @Override public boolean isSingleton() { return true; } public String getInterfaceName() { return interfaceName; } public void setInterfaceName(String interfaceName) { this.interfaceName = interfaceName; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public Object getProxyObj() { return proxyObj; } public void setProxyObj(Object proxyObj) { this.proxyObj = proxyObj; } }