Why is spring boot not finding my beans?

梦想与她 提交于 2019-12-11 10:47:22

问题


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.

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

It contains the following package scan:

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

im pretty sure im missing something here, please send help.

EDIT: injecting style:

private final ComposeCounterService composeCounterService;

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

回答1:


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.




回答2:


Did you use @Autowired on ComposeCounterService field?

If so; maybe can try using @ComponentScan above @SpringBootApplication

Documentation Here: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html

Hope this helps.

Cheers




回答3:


You need to add the package com.yyy.zzz.xxx.service in your scanBasePackages properties as your ControlService lies in this package.

Try this and let me know if you get any other issue

--Edit

Remove @Component from your interface ComposeCounterService (as interface never gets initialized)

Now give the bean name to your Service class as :

@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;
}

P.S: Make sure that all the pacakges are available in the component scan




回答4:


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. only took several hours of very angry debugging.



来源:https://stackoverflow.com/questions/51979275/why-is-spring-boot-not-finding-my-beans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!