Java getBean() 方法在这里做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38408014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:17:29  来源:igfitidea点击:

What does getBean() method do here?

javaspringgetter

提问by Arun Raaj

What is the getBean()method doing here and how does it work in a program?

getBean()这里的方法是什么以及它如何在程序中工作?

ApplicationContext aplicntxt = new 
ClassPathXmlApplicationContext("springconfig.xml");  

Hello h = (Hello) aplicntxt.getBean("springconfig.xml");  
h.display();

Hello h2 = new Hello();  //if I write this
h2.display();  

My question is why h2.displayretrieves null value and h.displayretrieves the stored values through springconfig.xml?

我的问题是为什么通过 springconfig.xmlh2.display检索空值并h.display检索存储的值?

Please tell me what does

请告诉我有什么作用

ApplicationContext aplicntxt = new ClassPathXmlApplicationContext("springconfig.xml");

do first?

先做?

Are all the values of xml file stored to the pojo class setters at first step?

第一步是否将 xml 文件的所有值都存储到 pojo 类设置器中?

Then we are storing the values to an object hby doing

然后我们h通过执行将值存储到对象

Hello h = (Hello) aplicntxt.getBean("springconfig.xml");

采纳答案by emotionlessbananas

From spring doc

来自 spring 文档

The interface org.springframework.context.ApplicationContextrepresents the Spring IoCcontainer and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.It allows you to express the objects that compose your application and the rich interdependencies between such objects.

接口org.springframework.context.ApplicationContext代表Spring IoC容器,负责实例化、配置和组装上述bean。容器通过读取配置元数据来获取有关要实例化、配置和组装哪些对象的指令。配置元数据表示在XML, Java annotations, or Java code.它允许您表达组成您的应用程序的对象以及这些对象之间丰富的相互依赖关系。

Several implementations of the ApplicationContext interfaceare supplied out-of-the-box with Spring. In standalone applications it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.While XML has been the traditional format for defining configuration metadata you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declarativelyenable support for these additional metadata formats.

ApplicationContext interfaceSpring 提供了开箱即用的几种实现。在独立的应用程序中,创建一个实例是很常见的,ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.虽然 XML 是定义配置元数据的传统格式,但您可以通过提供少量 XML 配置来指示容器使用 Java 注释或代码作为元数据格式来declaratively支持这些其他元数据格式。

Now Your Question and its simple ApplicationContextactivates the object(it is eager container) and looks for the beansdeclared so the objects are loaded whenever it is called.

现在你的问题及其简单ApplicationContext激活对象(它是急切的容器)并查找beans声明的对象,以便在调用时加载对象。

Now consider if you have two beans , and you need one of them you will find that bean by using ctx.getBean("beanId")to load instance and to provide data declared with this bean where beanIdwill tell which object to load.

现在考虑如果您有两个 bean,并且您需要其中一个,您将通过使用ctx.getBean("beanId")加载实例并提供用此 bean 声明的数据来找到该 bean,beanId它将告诉哪个对象load

consider following example

考虑下面的例子

<bean id="a" class="package.Hello" /> //here it will look for bean name and then loads the class

<bean id="a" class="package.Hello" /> //here it will look for bean name and then loads the class

Now when You call it like

现在当你称它为

ApplicationContext ctx=new ClassPathXMLApplicationContext("configuration.xml");

//will look for configuration.xml bean file if it is not in path then throw execption.

now

现在

Hello hello=ctx.getBean("a");

// if configuration.xml contains any bean named "a" and holds reference to class(hello) load it immediately and return object of that class

is same as

Hello hello=new Hello();
hello.method();

and it is creating object of Helloclass by using xml

它正在Hello通过使用创建类的对象xml

回答by Adam

Your question is essentially "How does spring work", this is covered extensively by the official documentation

您的问题本质上是“弹簧如何工作”,官方文档对此进行了广泛的介绍

The following creates all the beans defined by the springconfig.xml, that is it creates objects of the given types, and injects any properties you've defined, depending on your exact configuration it may also do things like package scanning, annotation processing etc.

下面创建由 springconfig.xml 定义的所有 bean,即它创建给定类型的对象,并注入您定义的任何属性,根据您的确切配置,它还可能执行包扫描、注释处理等操作。

ApplicationContext aplicntxt= new ClassPathXmlApplicationContext("springconfig.xml");

XML

XML

<bean class="org.example.Hello" id="foo" />
<bean class="org.example.Hello" id="bar" />

This would create an objects of type Hello and tag them with the IDs "foo" and "bar"

这将创建一个 Hello 类型的对象并用 ID“foo”和“bar”标记它们

All the beans are stored against their IDs for later retrieval via getBean(), note this takes the bean ID or name, not the XML file.

所有 bean 都根据它们的 ID 存储,以便以后通过 getBean() 检索,注意这需要 bean ID 或名称,而不是 XML 文件。

Hello h = (Hello) aplicntxt.getBean("foo");

回答by jencoston

From looking at this quickly, it appears that this code uses spring to initialize the Hello object with the values specified in the spring bean found in the xml file (I'm assuming that's what's in the filr, but I could be more specific if you post it).. When you create a second hello object you are using the constructor's default value for display, which is null.

从快速查看来看,该代码似乎使用 spring 来初始化 Hello 对象,并使用在 xml 文件中找到的 spring bean 中指定的值(我假设这就是 filr 中的内容,但我可以更具体,如果您发布它。。当您创建第二个 hello 对象时,您将使用构造函数的默认值进行显示,即空值。

回答by Mohammadreza Khatami

What you are doing in code is called Spring Dependency Injectionwhich let you define application beans and inject them when you need. like getBean()method that you use in your code which inject specific bean from XML file.

您在代码中所做的称为调用Spring Dependency Injection,它允许您定义应用程序 bean 并在需要时注入它们。像getBean()您在代码中使用的从 XML 文件注入特定 bean 的方法。