The problem that Spring Boot @Autowired cannot be automatically injected

Application startup class:

 

@SpringBootApplication
@EnableConfigurationProperties
@ComponentScan(basePackages = { "com.testing"})
public class Application {
   @Bean
   RestTemplate restTemplate() {
      return new RestTemplate();}
   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
      System.out.println (" Successfully started");
   }

Dao layers:

 

 

public interface UserRepository extends JpaRepository<User, String>{
    User findByUsername(String username);   //
}
Service layer: inject an interface that inherits JPA. In theory, spring boot will inject JPA into the repository. This interface must not need to implement classes.
The above method implements querying the User entity by Username. You can see that we have completed a conditional query method without any SQL-like statement here.
This is a great feature of Spring-data-jpa: create queries by parsing method names
 
@Service
public class DataInit {

    @Autowired
    UserRepository userRepository;
}

But it will prompt an error: Prompt that @Autowired cannot be injected normally:

 

It can be compiled normally, but cannot be run. When gradle bootrun, it prompts an error:

caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.testing.data.UserRepository com.testing.service.DataInit.userRepository; nested exception is org.spring
framework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.testing.data.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate
for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userRepository)}

 

Execution failed for task ':bootRun'.
> Process 'command 'C:\Program Files (x86)\Java\jdk1.8.0_101\bin\java.exe'' finished with non-zero exit value 1

Summarize:

When this type of error occurs, there are several issues that need to be checked:

1. Check whether various annotations are added, including @service, @repository, etc.; (note that @Autowired is placed on the service implementation, not the interface class.)

2. Whether the package is scanned correctly, this is very important! ! ! (my problem is because of this)

 

Note: http://bbs.csdn.net/topics/391978111?page=1#post-401966615 (#5)

The default rule of Bean assembly of SpringBoot project is to scan from top to bottom according to the package location where the Application class is located !

"Application class" refers to the SpringBoot project entry class. The location of this class is critical:
if the package where the Application class is located is: com.boot.app, only the com.boot.app package and all its subpackages will be scanned. If the package where the service or dao is located is not in com.boot.app and Under its sub-package, it will not be scanned!
That is,  put the Application class in the upper level of the package where dao and service are located , com.boot.Application

Knowing this is very important. I don't know if there are any instructions in the spring documentation. If you don't know, you can't solve it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970713&siteId=291194637