1. 程式人生 > 其它 >Spring高階--容器實現-ApplicationContext實現(一)

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>