1.IOC思想基于IOC容器完成, IOC容器底层就是对象工厂
2.Spring提供IOC容器实现的两种方式(两个接口):
1).BeanFactory: IOC容器基本实现方式, 是Spring内部使用的接口, 不提供开发人员进行使用, 加载配置文件是不会创建对象, 在获取对象(使用)时才去创建对象;
2).ApplicationContext: BeanFactory接口的子接口, 提供更多更强大的功能, 一般有开发人员进行使用, 加载配置文件时就会创建配置文件对象(这样就会让项目在开启时就创建好对象);
3. ApplicationContext接口有两个实现类:
两者的区别是:
1).FileSystemXmlApplicationContext的路径参数以项目路径开始(从项目目录开始写路径):
public class TestSpring5 { public static void main(String[] args){ //1.加载Spring配置文件 ApplicationContext context = new FileSystemXmlApplicationContext("conf/beans1.xml"); //2.获取配置创建的对象 User user = context.getBean("user", User.class); System.out.println(user); user.sayhi(); } }
2).ClassPathXmlApplicationContext的路径参数需写绝对路径(从盘符开始写, 前面加 file:):
public class TestSpring5 { public static void main(String[] args){ //1.加载Spring配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("file:D:\IntelliJIDEA\IntelliJ_IDEA_workspace\springDemo1015\conf\beans1.xml"); //2.获取配置创建的对象 User user = context.getBean("user", User.class); System.out.println(user); user.sayhi(); } }