为什么自动装配对于这个 Spring MVC String bean 属性不起作用?
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于您是通过 xml 显式指定属性值,因此不需要 AutoWired 注释。
Since you are explicitly specifying the property value via xml, there is no need to have the AutoWired annotation.
您没有正确使用自动装配功能。
您可以:
并为类中的属性添加 setter@Autowired
注释自动在 Spring 上下文中查找具有您的属性 class 的现有 bean(在您的情况下为String
)You are not using the autowire feature properly.
You can either:
<beans:property ... />
in the XML and add a setter for the property in the class@Autowired
annotation that automatically looks in the Spring context for an existing bean that has your property class (in your case,String
)其他答案是正确的,但为了澄清问题,*.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.
还有一个“其他答案是正确的”,但这里有另一个花絮给你——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.
如果您想自动装配您的属性,则需要为该 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.