简体   繁体   English

为什么 spring 引导找不到我的 bean?

[英]Why is spring boot not finding my beans?

i have following error:我有以下错误:

Parameter 0 of constructor in com.yyy.zzz.xxx.service.ControlService required a bean of type 'com.yyy.zzz.xxx.service.composeXML.ComposeCounterService' that could not be found.

Usually this is because i forget to annotate either the Service or the interface, but i've been looking classes the whole morning and cant find any missing annotations..通常这是因为我忘记注释服务或接口,但我整个上午都在看课,找不到任何遗漏的注释。

interface at this point is just:此时的界面只是:

@Component
public interface ComposeCounterService {
CLASSX init(List<YYY> owners) throws JAXBException;
}

and implimenting service is as follows, and contains init() method if that matters in this case.实现服务如下,如果在这种情况下重要,则包含 init() 方法。

@Service
public class ComposeCounterImpl implements ComposeCounterService {
/*** loots of code
}

and ApplicationConfig file is located one level above the service package. marked xxx in this post. ApplicationConfig 文件位于服务 package 的上一层。在本文中标记为 xxx。

It contains the following package scan:它包含以下 package 扫描:

@SpringBootApplication
scanBasePackages = {"com.yyy.zzz.xxx")

i also tried it with array of scans like:我还尝试了一系列扫描,例如:

scanBasePackages = {"com.yyy.zzz.xxx", "com.yyy.zzz.xxx.service.composeXML"})

and without the composeXML after.service None of these works.并且没有 composeXML after.service 这些都不起作用。

im pretty sure im missing something here, please send help.我很确定我在这里遗漏了一些东西,请发送帮助。

EDIT: injecting style:编辑:注入风格:

private final ComposeCounterService composeCounterService;

public ControlService(ComposeCounterService composeCounterService) {
    this.composeCounterService = composeCounterService;
}

the wrong import is: 错误的导入是:

import org.jvnet.hk2.annotations.Service;

correct is: 正确的是:

import org.springframework.stereotype.Service;

If you just let your IDE suggest the import and press enter without reading which one it adds, this is the result. 如果仅让您的IDE建议导入,然后按Enter键而不阅读添加的内容,那么就是结果。

You need to add the package com.yyy.zzz.xxx.service in your scanBasePackages properties as your ControlService lies in this package. 您需要在scanBasePackages属性中添加包com.yyy.zzz.xxx.service ,因为ControlService位于此包中。

Try this and let me know if you get any other issue 试试这个,让我知道是否还有其他问题

--Edit - 编辑

Remove @Component from your interface ComposeCounterService (as interface never gets initialized) interface ComposeCounterService删除@Component (因为接口永远不会初始化)

Now give the bean name to your Service class as : 现在,将Bean名称命名为Service类:

@Service("composeCounterImpl")
public class ComposeCounterImpl implements ComposeCounterService {
/*** loots of code
}

Now define your constructor as: 现在将您的构造函数定义为:

@Autowired
public ControlService(@Qualifier("composeCounterImpl") ComposeCounterService composeCounterService) {
    this.composeCounterService = composeCounterService;
}

PS: Make sure that all the pacakges are available in the component scan PS:确保组件扫描中的所有功能都可用

Did you use @Autowired on ComposeCounterService field? 您在ComposeCounterService字段上使用了@Autowired吗?

If so; 如果是这样的话; maybe can try using @ComponentScan above @SpringBootApplication 也许可以尝试使用@ComponentScan以上@SpringBootApplication

Documentation Here: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html 此处的文档: https : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html

Hope this helps. 希望这可以帮助。

Cheers 干杯

I am an absolute idiot... 我绝对是个白痴...

People, always check your imports... I even overlooked this and pasted only the code that would not solve the problem... 人们,总是检查您的进口商品...我什至忽略了这一点,只粘贴了无法解决问题的代码...

I had wrong import for @service annotation.. and thats the root cause of the problem. 我为@service批注导入了错误。这就是问题的根本原因。 only took several hours of very angry debugging. 只花了几个小时就非常生气地调试了。

I had a similar problem just now, where a specific bean couldn't be found when testing.我刚才遇到了类似的问题,测试时找不到特定的bean。 In my case the problem was a @WebMvcTest -annotation (wrapped inside a custom one, so I missed it).在我的例子中,问题是一个@WebMvcTest -annotation(包裹在一个自定义的里面,所以我错过了它)。 That annotation prevents SpringBoot from loading any components/beans except for those included here:该注释阻止 SpringBoot 加载除此处包含的组件/bean 之外的任何组件/bean:

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (ie @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component, @Service or @Repository beans).使用此注释将禁用完全自动配置,而是仅应用与 MVC 测试相关的配置(即@Controller、@ControllerAdvice、@JsonComponent、Converter/GenericConverter、Filter、WebMvcConfigurer 和 HandlerMethodArgumentResolver beans,但不包括 @Component、@Service 或 @Repository beans ).

You can force the loading of specific beans using the @Import -annotation like so:您可以使用@Import -annotation 强制加载特定 bean,如下所示:

@Import({ YourFirstBean.class, ThatOtherBean.class })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM