本文主要是介绍【Spring源码解读】BeanFactory和FactoryBean区别及类装载源码解读,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近读代码读到Bean装载过程,顺带上网搜了下BeanFactory和FactoryBean,发现好多文章都讲的不清不楚,特此自己来整理了一份BeanFactory和FactoryBean的区别及讲下bean的装载和读取过程的源码.
首先来看下BeanFactory和FactoryBean,借着例子作为入口来进行后面的源码分析.BeanFactory和FactoryBean的定义:
public interface FactoryBean<T> {T getObject() throws Exception;Class<?> getObjectType();boolean isSingleton();
}public interface BeanFactory {String FACTORY_BEAN_PREFIX = "&";//此属性后面会讲到Object getBean(String name) throws BeansException;<T> T getBean(String name, Class<T> requiredType) throws BeansException;<T> T getBean(Class<T> requiredType) throws BeansException;Object getBean(String name, Object... args) throws BeansException;<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;boolean containsBean(String name);boolean isSingleton(String name) throws NoSuchBeanDefinitionException;boolean isPrototype(String name) throws NoSuchBeanDefinitionException;boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;Class<?> getType(String name) throws NoSuchBeanDefinitionException;String[] getAliases(String name);
}
可以看到,二者的定义还比较相似,这里先直接说用法,实现FactoryBean
的类,在初始入容器后,通过BeanFactory
的getBean方法调用时,会调用FactoryBean
的getObject方法返回对应的类,而不是像普通的bean一样直接返回bean实例.BeanFactory的常量&的作用是在获取bean的时候直接返回FactoryBean的实例,而不是调用器的getObject方法返回对应的类,后面会有源码分析.
首先来看一个例子:
首先是具体对象:
public interface ICar {void automaker();
}
public class Benz implements ICar {public void automaker() {System.out.println("I'm Benz");}
}
public class VOLVO implements ICar {public void automaker() {System.out.println("I'm VOLVO");}
}
这里就是定义对象,不多讲,下面在applicationContext.xml中新增配置:
<bean id="carFactoryBean" class="org.white.test.web.test.CarFactoryBean" P:carEnum="BENZ"></bean>
下面来看测试代码:
public class CarFactoryBeanTest {private static final ApplicationContext CONTEXT = new ClassPathXmlApplicationContext("/META-INF/applicationContext.xml");@Testpublic void testFactoryBean() throws Exception {ICar car = (ICar) CONTEXT.getBean("carFactoryBean");car.automaker();CarFactoryBean carFactoryBean = (CarFactoryBean) CONTEXT.getBean("&carFactoryBean");carFactoryBean.getObject();}
}
运行结果:
I'm Benz
class org.white.test.web.test.CarFactoryBean
通过例子可以看到结果就如上面所说.老子说,知其然并要知其所以然.下面我们就来通读源码分析下bean的装载和读取.首先来看类图:
这里只讲部分核心代码,其他过程就省略掉了,我们直接来看DefaultBeanDefinitionDocumentReader
,此类实现自BeanDefinitionDocumentReader
,负责从传入的Element中解析出bean.Element的加载过程此处就不过多分析了,读者可以看上面类图,PathMatchingResourcePatternResolver
负责根据我们传入的xml路径解析为Resource,之后由DefaultDocumentLoader
获取Document,读者可以找代码看看.
//根据解析到的Element解析并注册具体bean
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {if (delegate.isDefaultNamespace(root)) {NodeList nl = root.getChildNodes();for (int i = 0; i < nl.getLength(); i++) {Node node = nl.item(i);if (node instanceof Element) {Element ele = (Element) node;if (delegate.isDefaultNamespace(ele)) {//解析默认标签parseDefaultElement(ele, delegate);}else {//解析自定义标签delegate.parseCustomElement(ele);}}}}else {delegate.parseCustomElement(root);}
}
上述代码中的parseCustomElement()方法是解析自定义标签,比如我们配置中的标签,就是spring扩展的自定义标签,会根据它的namespace即http://www.springframework.org/schema/context获取对应的处理器处理,这个今天就不展开了,留着下次专门写一篇自定义标签的文章详细给大家分析.
下面来看该类的另一个方法:
这里根据ele的不同类型做不同处理
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {//解析<import />属性并加载导入的xml里的beanif (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {importBeanDefinitionResource(ele);}//注册bean别名else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {processAliasRegistration(ele);}//注册具体beanelse if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {processBeanDefinition(ele, delegate);}//注册beanselse if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {// recursedoRegisterBeanDefinitions(ele);}
}
上面的方法解析出bean后要放入容器中,下面就是最核心的容器类DefaultListableBeanFactory
//bean容器,xml中的bean解析后会放入这里
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);//方法代码很多,其实很简单,就是将bean放入容器中
//实现自BeanDefinitionRegistry,注册具体bean,即将bean放入容器beanDefinitionMap中,后续取值从这里取
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)throws BeanDefinitionStoreException {Assert.hasText(beanName, "Bean name must not be empty");Assert.notNull(beanDefinition, "BeanDefinition must not be null");if (beanDefinition instanceof AbstractBeanDefinition) {try {((AbstractBeanDefinition) beanDefinition).validate();}catch (BeanDefinitionValidationException ex) {throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,"Validation of bean definition failed", ex);}}BeanDefinition oldBeanDefinition;oldBeanDefinition = this.beanDefinitionMap.get(beanName);if (oldBeanDefinition != null) {if (!isAllowBeanDefinitionOverriding()) {throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,"Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +"': There is already [" + oldBeanDefinition + "] bound.");}else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTUREif (this.logger.isWarnEnabled()) {this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +"' with a framework-generated bean definition: replacing [" +oldBeanDefinition + "] with [" + beanDefinition + "]");}}else if (!beanDefinition.equals(oldBeanDefinition)) {if (this.logger.isInfoEnabled()) {this.logger.info("Overriding bean definition for bean '" + beanName +"' with a different definition: replacing [" + oldBeanDefinition +"] with [" + beanDefinition + "]");}}else {if (this.logger.isDebugEnabled()) {this.logger.debug("Overriding bean definition for bean '" + beanName +"' with an equivalent definition: replacing [" + oldBeanDefinition +"] with [" + beanDefinition + "]");}}this.beanDefinitionMap.put(beanName, beanDefinition);}else {if (hasBeanCreationStarted()) {// Cannot modify startup-time collection elements anymore (for stable iteration)synchronized (this.beanDefinitionMap) {this.beanDefinitionMap.put(beanName, beanDefinition);List<String> updatedDefinitions = new ArrayList<String>(this.beanDefinitionNames.size() + 1);updatedDefinitions.addAll(this.beanDefinitionNames);updatedDefinitions.add(beanName);this.beanDefinitionNames = updatedDefinitions;if (this.manualSingletonNames.contains(beanName)) {Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames);updatedSingletons.remove(beanName);this.manualSingletonNames = updatedSingletons;}}}else {// Still in startup registration phasethis.beanDefinitionMap.put(beanName, beanDefinition);this.beanDefinitionNames.add(beanName);this.manualSingletonNames.remove(beanName);}this.frozenBeanDefinitionNames = null;}if (oldBeanDefinition != null || containsSingleton(beanName)) {resetBeanDefinition(beanName);}
}
上面就是初始化的bean装载过程.
下面来看bean的实例化:
看AbstractBeanFactory
类中如下代码:
protected Object getObjectForBeanInstance(Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());}//注意这里,如果我们的bean满足://1.没有实现FactoryBean 2.isFactoryDereference方法是判定是否以&符号开头//满足则直接返回实例.否则则会走下面的逻辑 所以这里就是BeanFactory中常量&的用法if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {return beanInstance;}Object object = null;if (mbd == null) {object = getCachedObjectForFactoryBean(beanName);}if (object == null) {FactoryBean<?> factory = (FactoryBean<?>) beanInstance;if (mbd == null && containsBeanDefinition(beanName)) {mbd = getMergedLocalBeanDefinition(beanName);}boolean synthetic = (mbd != null && mbd.isSynthetic());object = getObjectFromFactoryBean(factory, beanName, !synthetic);}return object;
}
上面方法会继续调用到下面方法:
private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)throws BeanCreationException {Object object;try {if (System.getSecurityManager() != null) {AccessControlContext acc = getAccessControlContext();try {object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {@Overridepublic Object run() throws Exception {//调用FactoryBean的getObject方法来返回实例return factory.getObject();}}, acc);}catch (PrivilegedActionException pae) {throw pae.getException();}}else {//调用FactoryBean的getObject方法来返回实例object = factory.getObject();}}catch (FactoryBeanNotInitializedException ex) {throw new BeanCurrentlyInCreationException(beanName, ex.toString());}catch (Throwable ex) {throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);}if (object == null && isSingletonCurrentlyInCreation(beanName)) {throw new BeanCurrentlyInCreationException(beanName, "FactoryBean which is currently in creation returned null from getObject");}return object;
}
上面注释已经说明了,最终其实会调用FactoryBean的getObject方法来返回实例.
总结:
- FactoryBean其实就是一个简单工厂,实现其方法覆写getObject方法可以直接简易的实现工厂模式.
- BeanFactory是Spring的核心接口,说白了其实也是采用了工厂模式,根据传入的不同bean名字,之后调用容器(如DefaultListableBeanFactory)返回具体的bean实例.我们常用的
ClassPathXmlApplicationContext
以及FileSystemXmlApplicationContext
等都实现了此接口.
欢迎关注个人博客:blog.scarlettbai.com
这篇关于【Spring源码解读】BeanFactory和FactoryBean区别及类装载源码解读的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!