码迷,mamicode.com
首页 > 编程语言 > 详细

Spring 源码阅读之BeanFactory

时间:2015-02-10 13:17:16      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

1. BeanFactory 的结构体系如下:

技术分享

2. XmlBeanFactory ,装载Spring配置信息

package org.springframework.beans.factory.xml;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.Resource;

public class XmlBeanFactory extends DefaultListableBeanFactory {

    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);


    /**
     * Create a new XmlBeanFactory with the given resource,
     * which must be parsable using DOM.
     * @param resource XML resource to load bean definitions from
     * @throws BeansException in case of loading or parsing errors
     */
    public XmlBeanFactory(Resource resource) throws BeansException {
        this(resource, null);
    }

    /**
     * Create a new XmlBeanFactory with the given input stream,
     * which must be parsable using DOM.
     * @param resource XML resource to load bean definitions from
     * @param parentBeanFactory parent bean factory
     * @throws BeansException in case of loading or parsing errors
     */
    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
        super(parentBeanFactory);
        this.reader.loadBeanDefinitions(resource);
    }

}

3. BeanFactory 接口

package org.springframework.beans.factory;

import org.springframework.beans.BeansException;


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;

    
    boolean containsBean(String name);

    
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

    
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

    
    boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException;

    
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;

    
    String[] getAliases(String name);

}

4. 运用实例:

Resource reource = null;//load spring configure file
BeanFactory bf = new XmlBeanFactory(reource);
bf.getBean("beanName");

Spring 源码阅读之BeanFactory

标签:

原文地址:http://www.cnblogs.com/tangyanbo/p/4283506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!