getBean()方法在这里做什么? [英] What does getBean() method do here?

查看:85
本文介绍了getBean()方法在这里做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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();  

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

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

请告诉我做什么

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

先做吗?

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

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

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

Then we are storing the values to an object h by doing

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

推荐答案

来自Spring文档

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

The interface org.springframework.context.ApplicationContext represents the Spring IoC container 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.

ApplicationContext interface的几种实现随Spring一起提供.在独立应用程序中,通常创建ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.的实例.XML是定义配置元数据的传统格式,您可以通过为declaratively启用对这些其他元数据格式的支持.

Several implementations of the ApplicationContext interface are 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 declaratively enable support for these additional metadata formats.

现在,您的问题及其简单的ApplicationContext激活对象(它是渴望的容器)并查找声明的beans,以便在调用该对象时立即加载该对象.

Now Your Question and its simple ApplicationContext activates the object(it is eager container) and looks for the beans declared so the objects are loaded whenever it is called.

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

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 beanId will tell which object to load.

考虑以下示例

<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

现在,当您称呼它时

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

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

现在

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

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

,它正在使用xml

这篇关于getBean()方法在这里做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆