ApplicationContext的继承体系
- applicationContext:接口类型,代表应用上下文,可以通过其实例获得 Spring 容器中的 Bean 对象
ApplicationContext的实现类
- ApplicationContext的继承体系
它是从类的根路径下加载配置文件 推荐使用这种
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
- FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
ApplicationContext app = new FileSystemXmlApplicationContext("E:\\gitee\\java-study\\spring\\ssc_spring_ioc\\src\\main\\resources\\applicationContext.xml");
- AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。
getBean方法
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService1 = (UserService) applicationContext.getBean("userService");
UserService userService2 = applicationContext.getBean(UserService.class);