这是我收到的错误消息
Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}] with root cause org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
更新:
这是我要自动装配的类,实际上它是带有参数化构造函数的服务类。
@Component
@Scope("prototype")
public class RelatedProductService {
private String aa;
private String bb;
private String cc;
@Autowired
public RelatedProductService(String aa, String aa, String orgId, String cc) {
super();
this.aa = aa;
this.bb = bb;
this.cc = cc;
}
...
}
这是我要使用的地方,控制器类。
@Controller("relatedProductsV1")
@RequestMapping("api/v1/related-products")
@Scope("prototype")
public class RelatedProductsController extends BaseController {
@Autowired
private RelatedProductService relatedProductService;
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public String getRelatedProducts( @RequestParam(value = "aa", required = true) String aa,
@RequestParam(value = "bb", required = true) String bb,
@RequestParam(value = "cc", required = true) String cc) {
...
String response = relatedProductService.getRelatedProductsResponse();
...
}
}
希望这会有所帮助。
答案 0 :(得分:0)
在没有太多上下文的情况下,您似乎正在尝试自动装配至少具有非默认构造函数(将String作为参数的bean)的bean。您应该:
答案 1 :(得分:0)
从第一类和@Autowired私有的RelatedProductService relatedProductService中删除注释;来自第二堂课,它应该可以工作
使用这种结构,您不会自动接线,因为您是自己启动“注入”服务的。
答案 2 :(得分:0)
首先感谢大家的宝贵想法/建议,高度赞赏他们。
我认为我首先犯了一个错误,那就是构造函数要注入作为用户请求参数传入的属性/构造函数组合,而当应用程序上下文加载时,这些确实不会存在。自动装配组件的构造函数的理想用法是当properties / constructor参数是将驻留在属性文件中的常量时。
所以我选择去找接洽人并解决了我的问题。我必须说,您的所有答复和协助都帮助我走了这条路。