1

I am writing a spring boot bases application. There are 5 such separate projects and all of the projects share 100+ java files in common and I decided to make a jar out of these common java files. I have followed below steps to achieve this:

  1. Created jar with the common files.(Some are annotated with @Component and some are not)
  2. Imported the jar into those 5 applications and used @ComponentScan annotation to scan both the imported jar package and depended project package. This did not work so I have tried installing the jar in local maven repo and adding pom dependency of jar in pom.xml of depended project and still it did not work.

The issue I am facing is even after using the jar in the project, spring boot gives below error:

    APPLICATION FAILED TO START

Description:

Field userService in main.java.rest.UsersController required a bean of type 'main.java.service.UserService' that could not be found.

Action:

Consider defining a bean of type 'main.java.service.Something' in your configuration.

But I have annotated Something.java with @Component.

I don't understand where am going wrong or if am missing something here. But it works when I write:

@Bean
public Something getSomething(){
 return new Something();
} 

But I cannot write such methods for 100+ components. Is there a way to get all beans scanned with one configuration class or any separate approach to achieve this?

Possible duplicate of 'Field required a bean of type that could not be found.' error spring restful API using mongodb, but I tried those approaches and still facing same error.

  • What does "Imported the jar into those 5 applications" mean exactly? – tgdavies Nov 18 '21 at 23:34
  • It means, if we need that jar in a project, we add the dependency in the pom.xml and the jar is being installed in the local maven repo using the command: mvn install:install-file by providing -D attributes like groupId, artifactId, version and jar location. I do the same thing for all projects. We can also import jar to the project using build path-> add external jar-> choose the jar. I did try these two approaches but still got the same error. – Upender Bikkumalla Nov 19 '21 at 02:58

0 Answers0