@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)}:
当它说没有找到依赖项的[java.lang.String]类型的匹配bean
时,我想知道是否还需要说些什么来让它知道值English
是一个字符串
?
最佳答案
由于您是通过 xml 显式指定属性值,因此不需要 AutoWired 注释。
关于java - 为什么 Autowiring 对于这个 Spring MVC String bean 属性不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6617315/