J.E.Y J.E.Y - 2 months ago 18
Java Question

@Service annotated bean wasn't picked up by SpringBootApplication

I used SpringBootApplication in a small project. The architecture is simple:

Entry file:

public class RptApp implements CommandLineRunner
@Autowired private RptService rptService;
public void run(String... args) throws Exception {

In which RptService is an interface and it has an implementation: RptServiceImpl.java. It's that the RptServiceImpl.java was annotated with @Service.

public class RptServiceImpl implements RptService {
public void doStuff();

My understanding is that @SpringBootApplication already embeds @ComponentScan, @EnableComponentScan (or something like that), @Configuration so that rptService should be automatically wired by the container. Rather it threw an error like:


Field RptService in XXXX.RptApp required a bean of type 'xxx.xxx.xxx.RptService' that could not be found.


Consider defining a bean of type 'xxx.xxx.xxx.RptService' in your configuration.

I know how to find the workaround basing on the hint, that besides the point though.

I did write another simple class Client and annotate it with @Component and @Autowired it in the main file. Spring didn't have problem picking that up.

The relevant part of my pom file looks like:



anyone might shed a light?


RptService should be in the main entry file's sub directory to be picked Spring @ComponentScan