问题
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