Spring高级--容器实现-ApplicationContext实现(一)

一、ClassPathXmlApplicationContext:从类路径查找 XML 配置文件,创建容器(旧)

1、代码

    /**
     * 较为经典的容器,基于classpath 下xml格式配置文件来创建
     */
    private static void testClassPathXmlApplicationContext(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("b01.xml");
        Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
        Bean1 bean1 = context.getBean(Bean2.class).getBean1();
        System.out.println(bean1);

    }

2、Bean

 static class Bean1{
    }

    static class Bean2{
        private Bean1 bean1;

        public void setBean1(Bean1 bean1){
            this.bean1=bean1;
        }

        public Bean1 getBean1(){
            return bean1;
        }
    }

3、用xml方式注入bean

<?xml version="1.0" encoding="UTF-8"?>
<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="bean1" class="com.mangoubiubiu.show.AplicontextInterface.Bean1"/>

    <bean id="bean2" class="com.mangoubiubiu.show.AplicontextInterface.Bean2">
     <property name="bean1" ref="bean1"/>
    </bean>

</beans>

结果都注入到了ioc容器,并进行了依赖注入

二、FileSystemXmlApplicationContext:从磁盘路径查找 XML 配置文件,创建容器(旧)

  /**
     * 基于磁盘下的xml格式配置文件来创建
     */
    private static void testFileSystemXmlApplicationContext(){

        FileSystemXmlApplicationContext context
                = new FileSystemXmlApplicationContext("E:\\idea_workspace_study\\show\\src\\main\\resources\\b01.xml");
        Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
        Bean1 bean1 = context.getBean(Bean2.class).getBean1();
        System.out.println(bean1);

    }

结果都注入到了ioc容器,并进行了依赖注入

小总结:

ClassPathXmlApplicationContext和FileSystemXmlApplicationContext是如何将xml里的bean注入进去的

1、ClassPathXmlApplicationContext

 public static void main(String[] args) {
        DefaultListableBeanFactory factory=new DefaultListableBeanFactory();
        Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
        System.out.println("----------------------------------------------");
        XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(new ClassPathResource("b01.xml"));

        Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
    }

2、FileSystemXmlApplicationContext

    public static void main(String[] args) {
        DefaultListableBeanFactory factory=new DefaultListableBeanFactory();
        Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
        System.out.println("----------------------------------------------");
        XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(new FileSystemResource("E:\\idea_workspace_study\\show\\src\\main\\resources\\b01.xml"));
        Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
    }

三、AnnotationConfigApplicationContext:加入一些有用的处理器

1、代码

    private static void testAnnotationConfigApplicationContext(){
        AnnotationConfigApplicationContext context=
                new AnnotationConfigApplicationContext(Config.class);
        Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
    }

2、用配置类注入Bean

 @Configuration
    static class Config{

        @Bean
        public Bean1 bean1(){
            return new Bean1();
        }
        @Bean
        public Bean2 bean2(){
            return new Bean2();
        }


    }

3、相当于<context:annotation-driven/>

没加之前

    private static void testClassPathXmlApplicationContext(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("b01.xml");
        Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{
            System.out.println(name);
        });
        Bean1 bean1 = context.getBean(Bean2.class).getBean1();
        System.out.println(bean1);
    }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="bean1" class="com.mangoubiubiu.show.AplicontextInterface.Bean1"/>

    <bean id="bean2" class="com.mangoubiubiu.show.AplicontextInterface.Bean2">
     <property name="bean1" ref="bean1"/>
    </bean>
</beans>
加了之后

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="bean1" class="com.mangoubiubiu.show.AplicontextInterface.Bean1"/>

    <bean id="bean2" class="com.mangoubiubiu.show.AplicontextInterface.Bean2">
     <property name="bean1" ref="bean1"/>
    </bean>
    <context:annotation-config/>
</beans>

 

 

 

 

 

 

 

posted @ 2022-04-01 23:03  KwFruit  阅读(96)  评论(0)    收藏  举报