[TOC]

BeanFacotry

  1. 实现
    • ListableBeanFactory:该接口定义了访问容器中Bean基本信息的若干方法,如查看Bean的个数、获取某一类型Bean的配置名、查看容器中是否包括某一个Bean等
    • ConfigurableBeanFactory:增强IOC容器的可定制性。定义了设置类装载器、属性编辑器、容器初始化后置处理器等方法
    • AutowireCapableBeanFacotry:定义了将容器中的Bean按某种规则(如名字匹配、类型匹配)进行自动装配的方法
    • BeanDefinitionRegistry:Spring配置文件中每一个节点元素在Spring容器里都通过一个BeanDefinition对象表示,它描述了Bean的配置信息。而BeanDefinitionRegistry接口提供了向容器手工注册BeanDefinition对象的方法

ApplicationContext

  1. ApplicationContext由BeanFactory派生而来,相比BeanFactory很多功能需要编程的方式实现,ApplicationContext中可以通过配置的方式实现
  2. ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
  3. BeanFactory在初始化容器的时候并未实例化Bean,只有第一次访问某个Bean的时候才实例化。而ApplicationContext则在初始化应用上下文时就实例化所有单实例的Bean
  4. AnnotationConfigApplicationContext基于注解类的配置
    public class TestAnnotationConfig {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeansConfig.class);
            Person person = ctx.getBean("person",Person.class);
            System.out.println(person);
        }
    }


    @Configuration
     class BeansConfig{
        @Bean(name="person")  
        public Person buildPerson(){
            Person person = new Person();
            person.setAge(100);
            person.setName("jannal");
            return person;
        }

    }

    class Person{
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }
5. WebApplicationContext 专门为web应用准备的,它允许从相对于web根目录的路径中装在配置文件完成初始化工作。从WebApplicationContext中可以获得ServletContext的引用,整个web应用上下文对象将作为属性放置到servletContext中,以便web应用环境可以访问Spring应用上下文。`WebApplicationContextUtils`通过此类的`getWebApplicationContext(ServletContext sc)`方法,可以从ServletContext中获取WebApplicationContext实例
6.

results matching ""

    No results matching ""