gpt4 book ai didi

java - 为什么我的示例中需要具有相同名称的 bean?为什么当我将 bean 文件的值更改为 bean 配置文件时,它永远不会返回我的输出?

转载 作者:行者123 更新时间:2023-12-01 12:24:57 28 4
gpt4 key购买 nike

我完全是Spring框架的初学者。我当时正在玩一个创建对象实例的示例。因此需要您的帮助!

看看这个例子:

MainApp.java:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp{
public static void main(String[] args){

ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj1=(HelloWorld) context.getBean("helloBd2");

obj1.setMessage("fooo..");
obj1.getMessage();

HelloWorld obj2=(HelloWorld) context.getBean("helloBd2");
obj2.getMessage();
}
}

第一个问题: 为什么这里的 bean id 需要相同的名称?否则会出现如下错误..

    Oct 18, 2014 1:28:44 PM 
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@1c12fb0: startup
date [Sat Oct 18 13:28:44 PDT 2014]; root of context hierarchy Oct 18, 2014 1:28:44 PM
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml] Exception in
thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean
named 'helloBd1' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641) at
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280) at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) at MainApp.main(MainApp.java:8)

好的,看看我的 beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="helloBd2" class="HelloWorld" scope="singleton">
<property name="message" value="foo with blah blah.."/>
</bean>

</beans>

第二个问题: 为什么这个程序返回的输出为..

Message: fooo..
Message: fooo..

它必须将我的输出返回为:

Message: fooo..
Message: foo with blah blah..

因为当我将我的值更改为 beans.xml 时,它必须与 beans 配置文件一起使用,不幸的是,没有发生这种情况!为什么?

请帮忙!

帮助将不胜感激!

最佳答案

我不知道你从哪里得到

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloBd1' is defined at

您的问题中没有任何内容涉及 helloBd1 bean。

至于输出,您已将 bean 声明为

<bean id="helloBd2" class="HelloWorld" scope="singleton">

也就是说,它具有单例范围。这意味着两者

HelloWorld obj1=(HelloWorld) context.getBean("helloBd2");
HelloWorld obj2=(HelloWorld) context.getBean("helloBd2");

正在检索您最初调用的同一对象

obj1.setMessage("fooo..");

大概将消息更改为fooo..

关于java - 为什么我的示例中需要具有相同名称的 bean?为什么当我将 bean 文件的值更改为 bean 配置文件时,它永远不会返回我的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com