ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
UserServiceImpl:
package com.by.service;import com.by.dao.UserDao;/*** 业务层实现类*/
public class UserServiceImpl implements UserService {private UserDao userDao;public UserServiceImpl() {System.out.println("使用构造方法创建对象");}}
测试:在main方法中
new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println("spring ioc 容器创建好了");
结果:
BeanFactory:是在 getBean 的时候才会创建对象。
UserServiceImpl:
package com.by.service;import com.by.dao.UserDao;/*** 业务层实现类*/
public class UserServiceImpl implements UserService {private UserDao userDao;public UserServiceImpl() {System.out.println("使用构造方法创建对象");}
}
测试:在main方法中
//BeanFactory在getBean是才会创建XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));UserService userService = (UserService) beanFactory.getBean("userService");//System.out.println(userService);System.out.println("spring ioc 容器创建好了");
结果展示: