三、获取Spring容器及获取容器中的Bean对象

  Spring中的容器对象接口是ApplicationContext,其作用就是加载配置文件,并初始化所有的Bean对象(容器启动时)。其实现类主要有2个,分别为:

ClassPathXmlApplicationContext:加载类路径下的Spring配置文件
FileSystemXmlApplicationContext:加载本地磁盘的Spring配置文件

  在Spring容器启动后,即可获取Bean对象:


        //加载Spring配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("org.pc");
        //获取Bean对象
        applicationContext.getBean(User.class);

猜你喜欢

转载自blog.csdn.net/panchang199266/article/details/81913679