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>
复制代码

 

 

 

 

 

 

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/16089617.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(79)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起