简体   繁体   中英

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<\/code> of my project this problem appears:

***************************
APPLICATION FAILED TO START
***************************

**Description**:

Field userService in com.example.accessingdatamysql.rest.MainController required a bean of type 'com.example.accessingdatamysql.service.UserService' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.accessingdatamysql.service.UserService' in your configuration.

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 :

@SpringBootApplication (scanBasePackages = "com.example.accessingdatamysql")

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 :

 @Autowired 
         
private UserService userService;

With this :

 @Autowired          
 private UserService userService;

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).

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.3.1.Final</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>
                        -Amapstruct.defaultComponentModel=spring
                    </compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

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

import java.sql.Timestamp;




private Timestamp timestamp;

public Timestamp getTimestamp() {
    return timestamp;
}

public void setTimestamp(Timestamp timestamp) {
    this.timestamp = timestamp;
}

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 :

封装结构

And your classes arranged in the below way :

包中的类

You need to define a bean to let the Spring use them such as DI(Dependency Injection) @Autowired in this case.

There are various ways to define the beans but in your case using @Service annotation on the service class so that Spring can find it during the initialization.

And you don't need to declare @ComponentScan annotation the @SpringbootApplication will handle everything.

As @Som said, you need to implement your Userservice class since it is interface and there is no implemented class yet.

Try to remove @ComponentScan annotation and implement UserService interface as follow

@Service
public class UserServiceImpl implements UserService{
    // the rest of the code
}

hi I'm facing the same error in console



hi facing the same error



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM