2

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;
}
Clomez
  • 1,410
  • 3
  • 21
  • 41
  • 1
    Show how you Autowire it. Btw you don't need Component annotation on service, and you don't need scanBasePackages, SpringBoot automatically scan for basePackage. – Moler Aug 23 '18 at 06:14
  • Added. Also you mean i dont need the "at"Service, since i have "at"Component on the interface which the service impliments? I know i dont need scanBasePackages but at this point i added it to be sure, but good to know it should work without! @Moler – Clomez Aug 23 '18 at 06:19
  • 1
    Sorry I meant, you don't need component it at your interface. You only need Service annotation on your ComposeCounterImpl. – Moler Aug 23 '18 at 06:33
  • Check, nice to know – Clomez Aug 23 '18 at 06:39

5 Answers5

6

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.

Repe
  • 96
  • 5
2

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.

Clomez
  • 1,410
  • 3
  • 21
  • 41
1

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

codeLover
  • 2,571
  • 1
  • 11
  • 27
1

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

JWiryo
  • 371
  • 4
  • 11
0

I had a similar problem just now, where a specific bean couldn't be found when testing. In my case the problem was a @WebMvcTest-annotation (wrapped inside a custom one, so I missed it). That annotation prevents SpringBoot from loading any components/beans except for those included here:

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e. @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component, @Service or @Repository beans).

You can force the loading of specific beans using the @Import-annotation like so:

@Import({ YourFirstBean.class, ThatOtherBean.class })
kiloton
  • 105
  • 2
  • 10