spring两种装配模式

2019-01-01  本文已影响0人  毛仑上保罗先生
@Test
    public void getBean() throws Throwable {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("classpath:beans.xml");
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(resource);
        Car car = factory.getBean("car1", Car.class);
        System.out.println(car.getBrand());


        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Car car1 = applicationContext.getBean("car1", Car.class);
        System.out.println(car1.getBrand());
    }
上一篇下一篇

猜你喜欢

热点阅读