为什么自动装配对于这个 Spring MVC String bean 属性不起作用?

发布于 2024-11-19 00:36:55 字数 1140 浏览 3 评论 0原文

@Autowired
private String defaultLanguage;

当我尝试使用 @Autowire CountryBean 类的 defaultLanguage 字段时,如下所示:

<beans:bean id="countryBean" class="geoapp.CountryBean">
    <beans:property name="defaultLanguage" value="English" />
</beans:bean>

我收到此错误:

Error creating bean with name 'CountryBean': 
Injection of autowired dependencies failed; 
nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: 
private java.lang.String geoapp.CountryBean.defaultLanguage; 
nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No matching bean of type [java.lang.String] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for 
this dependency. 
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}:

当它显示 Nomatching 时找到依赖项 [java.lang.String] 类型的 bean,我想知道是否还需要说一些其他内容来让它知道值 English 是一个 String< /代码>?

@Autowired
private String defaultLanguage;

When I try to @Autowire the defaultLanguage field of the CountryBean class as follows:

<beans:bean id="countryBean" class="geoapp.CountryBean">
    <beans:property name="defaultLanguage" value="English" />
</beans:bean>

I get this error:

Error creating bean with name 'CountryBean': 
Injection of autowired dependencies failed; 
nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: 
private java.lang.String geoapp.CountryBean.defaultLanguage; 
nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No matching bean of type [java.lang.String] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for 
this dependency. 
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}:

When it says No matching bean of type [java.lang.String] found for dependency, I wonder if there's something else I need to say to let it know that the value English is a String?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

唔猫 2024-11-26 00:36:55

由于您是通过 xml 显式指定属性值,因此不需要 AutoWired 注释。

Since you are explicitly specifying the property value via xml, there is no need to have the AutoWired annotation.

少年亿悲伤 2024-11-26 00:36:55

您没有正确使用自动装配功能。
您可以:

  • 在 XML 中使用 并为类中的属性添加 setter
  • 使用 @Autowired 注释自动在 Spring 上下文中查找具有您的属性 class 的现有 bean(在您的情况下为 String

You are not using the autowire feature properly.
You can either:

  • Use the <beans:property ... /> in the XML and add a setter for the property in the class
  • Use the @Autowired annotation that automatically looks in the Spring context for an existing bean that has your property class (in your case, String)
简美 2024-11-26 00:36:55

其他答案是正确的,但为了澄清问题,*.xml 文件中定义的那些 bean 在处理任何 @ 类型注释之前进行处理。因此,对于您的示例,bean“CountryBean”在处理属性“defaultLanguage”之前被处理,从而使依赖项为空。

The other answers are correct, but to clarify things, those beans defined in the *.xml files are processed before any @ type annotations get processed. So for your example, the bean "CountryBean" is processed before the property "defaultLanguage" is processed, leaving the dependency null.

东走西顾 2024-11-26 00:36:55

还有一个“其他答案是正确的”,但这里有另一个花絮给你——Spring会自动为你设置枚举,所以如果你有一个语言枚举并且你的bean定义有类似“的东西,你会得到一个类型安全的那里有枚举,而不是字符串。

Yet another 'the other answers are correct', but here's another tidbit for you -- Spring will automatically set enums for you, so if you had an enum of languages and your bean definition had something like ", you'd get a type safe enum in there, instead of a String.

如果没结果 2024-11-26 00:36:55

如果您想自动装配您的属性,则需要为该 defaultLanguage 字符串创建一个新 bean。然后将 bean 的名称和字段的名称等同起来。

If you want autowiring your property, you need to make a new bean for that defaultLanguage String. Then equate the bean's name and field's name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文