[英]spring how to get BeanFactory from Application Context
I am a beginner in spring.我是 spring 的初学者。 Today, when I was reading the spring core document, I found the following instructions in chapter 1.3.今天在阅读spring核心文档的时候,发现1.3章有如下说明。 Bean Overview 豆概述
In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container (by users).除了包含有关如何创建特定 bean 的信息的 bean 定义之外,ApplicationContext 实现还允许注册在容器外部(由用户)创建的现有对象。 This is done by accessing the ApplicationContext's BeanFactory through the getBeanFactory() method, which returns the BeanFactory DefaultListableBeanFactory implementation.这是通过 getBeanFactory() 方法访问 ApplicationContext 的 BeanFactory 来完成的,该方法返回 BeanFactory DefaultListableBeanFactory 实现。 DefaultListableBeanFactory supports this registration through the registerSingleton(..) and registerBeanDefinition(..) methods. DefaultListableBeanFactory 通过 registerSingleton(..) 和 registerBeanDefinition(..) 方法支持这种注册。 However, typical applications work solely with beans defined through regular bean definition metadata.但是,典型的应用程序仅使用通过常规 bean 定义元数据定义的 bean。
I instantiated an ApplicationContext object in IDEA with the following code, but I found that this object does not have a getBeanFactory()
method:我用下面的代码在IDEA中实例化了一个ApplicationContext object,但是我发现这个object没有getBeanFactory()
方法:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test.xml");
The document I read is the 5.3.5 version of spring, and I am using the same version of spring.我看的文档是5.3.5版本的spring,我用的是同版本的spring。
I guess if spring changed the ApplicationContext class and forgot to modify the document.我猜如果 spring 更改了 ApplicationContext class 并忘记修改文档。 Or is there something wrong with the object I instantiated?还是我实例化的 object 有问题?
Thanks in advance!提前致谢!
I think you should be able to cast it to a ConfigurableApplicationContext.我认为您应该能够将其转换为 ConfigurableApplicationContext。
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ConfigurableApplicationContext.html https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ConfigurableApplicationContext.ZFC35FDC70D5FC69D2639883A822C7A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.