遇到了一个问题,在项目中启动调试时,发现报错No match bean错误,报错信息如下:
com.xxx.xxx.framework.exception.UnknowGeneralException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendSMSService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.xxx.xxx.module.xxx.server.service.IConfigurationParamsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(mappedName=, shareable=true, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)} at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
看报错信息知道是sendSMSService注入IConfigurationParamsService时发生,检查代码后发现,IConfigurationParamsService已经在配置文件中注入,并且在同包下的其他类中可以正常使用,并且按照正常来说,如果是因为未配置的bean,应该在启动时报错,而不是在运行时报错,一时间搞不清楚问题在哪。
后来再次检查代码,发现sendSMSService不是通过注解或者配置文件的方式注入的,而且在获取此service时,使用的是applicationContext.getBean(‘sendSMSService’)方法,而IConfigurationParamsService是引用的jar包中的service,在applicationContext中不能扫到此service,所以报错,至此,问题就好解决了。
于是在调用sendSMSService方法的地方,先使用@Resource标签注入IConfigurationParamsService,并把此service作为参数传入,问题解决。