spring自动装配bean失败报错:org.springframework.beans.factory.BeanCreationException: Could not autowire field
报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.context.support.ResourceBundleMessageSource com.yanglun.crm.handler.UserHandler.messageSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.context.support.ResourceBundleMessageSource] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.context.support.ResourceBundleMessageSource] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:530)
... 24 more
- 报错之前,是在调试使用i18n的过程中,加入i18n之前运行正常,加入之后出现了报错
- 对应的java代码文件如下:
加入i18n的过程中,基于原来的代码新添加了:
@Autowired
private ResourceBundleMessageSource messageSource;
@RequestMapping("/user")
@Controller
public class UserHandler {
@Autowired
private UserService userService;
@Autowired
private ResourceBundleMessageSource messageSource;
- 同时在spring-mvc.xml文件中添加了i18n的对应配置
<!-- 配置国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
-
基于该问题,主要根据如下几点进行排查:
1)检查注解正确的添加,
2)包引入是否正确,
3)检查web.xml监听器的配置是否正确 -
检查
1)注解正常:参考步骤2中的代码部分
2)包引入正确:参考步骤3中的配置信息和如下java类中包的引用
import org.springframework.context.support.ResourceBundleMessageSource;
3)检查web.xml监听器的配置:发现存在问题
web.xml中配置如下:
加载bean使用的对应config为applicationContext.xml
<!-- 加载spring bean -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
而对应的applicationContext.xml文件中配置如下:
问题就在于还有一个spring-mvc.xml的配置文件没有导入
<!--使用注解方式注入 -->
<context:annotation-config />
<!--自动扫描包 base-package为你的代码的路径-->
<context:component-scan base-package="com.yanglun.crm.*" />
<!--导入dao配置 -->
<import resource="spring-dao.xml" />
<!--导入数据库配置 -->
<import resource="spring-db.xml" />
<!--导入事务管理器配置 -->
<import resource="spring-tx.xml" />
</beans>
resources下的配置文件列表:
而spring-mvc.xml中,是刚刚添加了i18n的配置信息的,如果不关联上,是没有办法自动装配这个对应的bean的
解决:
在applicationContext.xml中添加配置,导入spring-mvc.xml,
<import resource="spring-mvc.xml" />
重新运行,问题解决
其它说明
而为什么,在之前applicationContext.xml 中没有引用spring-mvc.xml的情况下,spring-mvc.xml的有些配置还是会生效呢?
例如:spring-mvc.xml中的如下部分配置生效了
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- SpringMVC 的两个标准配置 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:view-controller path="/success" view-name="success"/>
<mvc:view-controller path="/index" view-name="index"/>
原因是,它们走的是web.xml中如下配置信息:
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这里web.xml 中定义了servlet对应配置文件名称为
spring-mvc.xml ,就是说如果是servlet相关的,就去spring-mvc.xml中找对应的信息;
但这里并没有说spring的bean的自动装配也走spring-mvc.xml,
实际上这里spring的bean的自动装配走的是applicationContext.xml,再通过applicationContext.xml去关联spring-mvc.xml,
spring-mvc.xml内部再配置i18n相关的信息,让spring进行自动装配
<!-- 配置国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)