@Test public void demo2(){ //使用Spring的工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //使用工厂获得类,连着属性一起获得 UserService userService = (UserService)applicationContext.getBean("userService"); userService.sayHello(); } @Test public void demo3(){ ApplicationContext applicationContext = new FileSystemXmlApplicationContext("d:\\applicationContext.xml"); UserService userService = (UserService)applicationContext.getBean("userService"); userService.sayHello(); } /** * 传统工厂方式:BeanFactory */ @Test public void demo4(){ BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); UserService userService = (UserService)beanFactory.getBean("userService"); userService.sayHello(); } /** * 传统工厂方式:BeanFactory-读取本地磁盘 */ @Test public void demo5(){ BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("d:\\applicationContext.xml")); UserService userService = (UserService)beanFactory.getBean("userService"); userService.sayHello(); }
Spring工厂类~
最新推荐文章于 2024-01-04 20:20:36 发布