Error on moving ServletInitizalier and HplusApplication java files to Config package | Spring Boot and MicroServices Forum
Sailee Naik Posted on 02/04/2020

hi, and files were present under test package. 

I moved them to com.test.config package where file is present just for better management of my java files

After i moved above mentioned two files i started getting an weird error message indicated below

Field userRepository in com.test.controllers.LoginController required a bean of type 'com.test.repository.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Consider defining a bean of type 'com.test.repository.UserRepository' in your configuration.

Now this error message is not correct since everything is in place properly in repository package for

When i moved those two files ( and ) out of com.test.config package back to com.test package the error went away and my project started working agin

I am not sure why this has happened- can you help me understand .... 


Yogesh Chawla Replied on 02/04/2020

This is because we need to set the classpath right and this is how Spring understands everything.

Don't use the “default” Package

When a class does not include a package declaration, it is considered to be in the “default package”. The use of the “default package” is discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the @ComponentScan, @EntityScan, or @SpringBootApplication annotations since every class from every jar is read.
I recommend that you follow Java’s recommended package naming conventions and use a reversed domain name for ex. com.test.interceptors etc.

Typical Layout

Spring boot team generally recommend that you locate your main application class in a root package above other classes.
The @SpringBootApplication annotation is placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @SpringBootApplication annotated class is used to search for @Entity items. Using a root package also allows the component scan to apply only on your project(NOTE).

Read this:

/src/main/java and /src/main/resources is a Maven project structure convention.
It's a path inside your project where you place java files and resources.
During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath

We can choose to build application by ourself or with a different build tool.
In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.

The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.

The classpath also contains additional libraries (JARs), which also can have a static folder, which would then be included for serving static resources.