Spring相关API

ApplicationContext继承体系

applicationContext:接口类型,代表应用上下文,通过其实例获得Spring容器中的Bean对象

ApplicationContext的实现类

  • ClassPathXmlApplicationContext
    从类的根路径下加载配置文件推荐使用
    ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

  • FileSystemXmlApplicationContext
    从磁盘路径上加载配置文件,配置文件可在磁盘任意位置
    ApplicationContext app=new ClassPathXmlApplicationContext("C:\\");

  • AnnotatonConfigXmlApplicationContext
    当使用注解配置容器对象时,需要使用此类来创建spring容器。它用来解读注解

getBean()方法

容器中某个类型对象有多个情况下,使用
UserService userService=(UserService)app.getBean("userService")
//""内可以区分对象id
容器中某个类型对象只有1个,通过class获取,不用强转
UserService userService=app.getBean(userService.class)

image.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容