0
点赞
收藏
分享

微信扫一扫

ApplicationContext实现

丹柯yx 2022-03-25 阅读 44
java

testDefaultListableBeanFactory

public class A02Application {
    public static void main(String[] args) {

        testDefaultListableBeanFactory();

        //testClassPathXmlApplicationContext();

        //testFileSystemXmlApplication();

        //testAnnotationConfigApplicationContext();
    }

    private static void testDefaultListableBeanFactory(){
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        System.out.println("读取之前.......");
        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println("BeanName:" + name);
        }
        // 找到bean定义的xml,把里面的每个标签转换为bean定义信息
        System.out.println("读取之后.......");
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        // 方式一:读取bean配置文件xml
        // reader.loadBeanDefinitions(new ClassPathResource("a02.xml"));
        // 方式二:读取bean配置文件xml
        reader.loadBeanDefinitions(new FileSystemResource("src/main/resources/a02.xml"));
        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println("BeanName:" + name);
        }


    }

  
    static class Bean1 {

    }

    static class Bean2 {
        private Bean1 bean1;

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

        public Bean1 getBean1() {
            return bean1;
        }
    }
}
读取之前.......
读取之后.......
Disconnected from the target VM, address: '127.0.0.1:51049', transport: 'socket'
10:31:39.391 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from file [C:\Users\Admin\Desktop\applicatioinContext\src\main\resources\a02.xml]
BeanName:bean1
BeanName:bean2

举报

相关推荐

0 条评论