Skip to content
Advertisement

Spring Boot @Mapper Bean creation issue : Application Failed to start. Error : Consider defining a bean of Type

I am new to spring, when i try to do the mvn clean install of my project this problem appears:

Error

JavaScript

The problem is that in the MainController there is the import of “UserService”:

JavaScript

It is probably a trivial thing but I can not bypass the problem, below I insert “UserService” and the MainStart

UserService.java

JavaScript

UPDATE : I insert the UserServiceImpl and the new main and Mapper, with the new error.

UserServiceImpl.java

JavaScript

AccessingDataMysqlApplication.java

JavaScript

UserMapper.java

JavaScript

New Error:

JavaScript

POM

JavaScript

Advertisement

Answer

Annotate your UserService class implementation [like UserServiceImpl.java] with @Service or @Component. Also make sure this class is located in a sub-package.

This is your main class package : com.example.accessingdatamysql Your UserService class and all other classes should be kept in a package like : com.example.accessingdatamysql.xxxxxx. Ensure this strategy is followed.

Plus remove the unnecessary annotations on your main class. The @SpringBootApplication annotation is equivalent to using the below 3 : :

  1. @Configuration,
  2. @EnableAutoConfiguration and
  3. @ComponentScan with attributes.

This will be enough :

JavaScript

And do not keep a gap when you autowire any bean injection. This does not cause any harm. But your code should be properly organized and indentation done.

Also replace below :

JavaScript

With this :

JavaScript

UPDATE-1

Do a maven clean install after you fix your spring boot configurations.

mvn clean install

UPDATE-2

Your bean for Mapper does not fully qualify for a spring bean. You need to compile your project with the below plugin (see the 2nd plugin I have used).

JavaScript

Then you need to fix your UserDto.java as below (change the type of timestamp variable else Mapper will fail):

JavaScript

Your main class should only have this : @SpringBootApplication (scanBasePackages = "com.example.accessingdatamysql") and no other annotation.

Then save your project. And Then run : mvn clean install -X

Make your package structure like this :

Package-Structure

And your classes arranged in the below way :

Classes-in-packages

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement