Skip to content

相关API

java
// Spring相关的API
ApplicationContext app = new ClassPathXmlApplicationContext(configLocation:"applicationContext.xml");
UserService userService = (UserService) app.getBean(s:"userService");
userService.save();  // 调用save()方法

ApplicationContext的继承体系:ApplicationContext是一个接口,代表应用上下文,可以通过其实例获得Spring容器中的Bean对象,ClassPathXmlApplicationContext是该接口对应的接口实现,使用多态的方式接收,ApplicationContext接口的所有实现类有:

  • ClassPathXmlApplicationContext:从类的根路径(resources文件夹)下加载配置文件

    java
    new ClassPathXmlApplicationContext(configLocation:"applicationContext.xml");
  • FileSystemXmlApplicationContext:从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置

    java
    new FileSystemXmlApplicationContext(configLocation:"D:\\src\\mian\\resources\\applicationContext.xml");
  • AnnotationConfigApplicationContext:使用注解配置容器对象时,需要使用此类来创建Spring容器,从而读取注解

getBean()

getBean()的参数有两种方式:

  • getBean("id")

    如:UserService userService = (UserService) app.getBean(s:"userService");

    当参数的数据类型是字符串时,表示根据配置文件中Beanid从容器中获得Bean实例,返回的是Object,需要进行强转

  • getBean(class)

    如:UserService userService = app.getBean(UserService.class);

    当参数的数据类型是Class类型时,表示根据类型从容器中匹配Bean实例,但是当容器中相同类型的Bean有多个时(即id不同,class的内容相同),则此方法会报错。

Released under the MIT License.