Spring相关API
1.ApplicationContext的实现类
1.1.ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件推荐使用这种。
1.2.FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
1.3.AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建spring容器。它用来读取注解。
2.getBean() 方法使用
Test:
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = app.getBean(UserService.class);
userService.save();
}
注意:
- 如果容器中的某一类型的Bean存在多个,则使用 getBean(id)
- 如果容器中的某一类型的Bean只存在一个,则使用
getBean(class)