java - 为什么 Autowiring 对于这个 Spring MVC String bean 属性不起作用?

标签 java spring spring-mvc autowired

@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/

相关文章:

java - Eclipse 无法创建就地编辑器

java - @Controller 、 @RestController 和 @Component 在 Spring boot 多模块 maven 项目的子包中不起作用

使用 '*' 通配符的 Spring CORS 映射不起作用

java - MongoDb 在java中比较同一集合中的两个字段

mysql - Spring Boot 应用程序出现 SQL 异常

java - 在典型的 MVC 应用程序中,哪一层负责 Model->DTO 转换?

java - 从不属于 ButtonGroup 的 JRadioButton 获取文本

java - Quartz 作业中的 Spring 应用程序 : obtaining server path,

java - Spring JPA "And"方法且不为空

java - 多态性和静态方法