码迷,mamicode.com
首页 > 移动开发 > 详细

spring BeanFactory和ApplicatContext

时间:2019-01-12 22:55:03      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:hide   c51   contains   ade   iss   try   except   color   div   

spring容器

BeanFactory提供了IoC的功能;ApplicatContext建立在BeanFactory的基础上


BeanFactory顶层接口

技术分享图片
package org.springframework.beans.factory;

import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;

public interface BeanFactory {
    String FACTORY_BEAN_PREFIX = "&";
    // getBean()方法从容器中返回Bean
    Object getBean(String var1) throws BeansException;

    <T> T getBean(String var1, Class<T> var2) throws BeansException;

    Object getBean(String var1, Object... var2) throws BeansException;

    <T> T getBean(Class<T> var1) throws BeansException;

    <T> T getBean(Class<T> var1, Object... var2) throws BeansException;
    // 容器里有没有一个Bean
    boolean containsBean(String var1);
    // 一个Bean是不是单例的
    boolean isSingleton(String var1) throws NoSuchBeanDefinitionException;
    // 一个Bean是不是复例的
    boolean isPrototype(String var1) throws NoSuchBeanDefinitionException;

    boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException;

    boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException;

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

    String[] getAliases(String var1);
}
View Code

BeanFactory的子接口ListableBeanFactory

技术分享图片
package org.springframework.beans.factory;

import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;

public interface ListableBeanFactory extends BeanFactory {
    // 是否包含特定的Bean
    boolean containsBeanDefinition(String var1);
    // 查看Bean的数量
    int getBeanDefinitionCount();
    
    String[] getBeanDefinitionNames();

    String[] getBeanNamesForType(ResolvableType var1);

    String[] getBeanNamesForType(Class<?> var1);

    String[] getBeanNamesForType(Class<?> var1, boolean var2, boolean var3);

    <T> Map<String, T> getBeansOfType(Class<T> var1) throws BeansException;

    <T> Map<String, T> getBeansOfType(Class<T> var1, boolean var2, boolean var3) throws BeansException;

    String[] getBeanNamesForAnnotation(Class<? extends Annotation> var1);

    Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> var1) throws BeansException;

    <A extends Annotation> A findAnnotationOnBean(String var1, Class<A> var2) throws NoSuchBeanDefinitionException;
}
View Code

BeanFactory的子接口HierarchicalBeanFactory

通过这个接口Spring的IoC容器可以建立父子层级关联的容器体系,子容器可以访问父容器中的Bean,但父容器不能访问子容器中的Bean。


AutowireCapableBeanFactory接口

定义了自动装配


DefaultSingletonBeanRegistry接口

DefaultSingletonBeanRegistry里有一个addSingleton()方法,会把单例的Bean保存到一个ConcurrentHashMap

    // 缓存到一个map里
    private final Map<String, Object> singletonObjects = new ConcurrentHashMap(256); 

    protected void addSingleton(String beanName, Object singletonObject) {
        Map var3 = this.singletonObjects;
        synchronized(this.singletonObjects) {
            // 添加单对象到map
            this.singletonObjects.put(beanName, singletonObject != null ? singletonObject : NULL_OBJECT);
            this.singletonFactories.remove(beanName);
            this.earlySingletonObjects.remove(beanName);
            this.registeredSingletons.add(beanName);
        }
    }

 

 

 


DefaultListableBeanFactory 实现类

这个类实现了上面所有的接口

技术分享图片
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="tank" class="my.Tank"></bean>

</beans>
View Code
技术分享图片
package my;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

public class MyTest {
    public static void main(String[] args) throws IOException {
        // 创建一个资源加载器
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 使用ClassPathResource加载配置文件
        Resource resource = resolver.getResource("classpath:my.xml");

        // 创建一个BeanFactory的对象
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(resource);

        // 单例的Bean已经缓存到容器中
        Tank tank = (Tank)factory.getBean(Tank.class);
        tank.run();
    }
}
class Tank {
    public void run() {
        System.out.println("run...");
    }
}
View Code

 

spring BeanFactory和ApplicatContext

标签:hide   c51   contains   ade   iss   try   except   color   div   

原文地址:https://www.cnblogs.com/Mike_Chang/p/10261122.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!