Spring Boot @Autowired not able to automatically inject problems

Source: Internet
Author: User

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 ("successful Start");   }

DAO Layer:

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 repository, the interface must not need to implement the class.
The above method realizes to query the user entity by username, we can see that we do not have any class SQL statements to complete a conditional query method.
That's one of the big features of SPRING-DATA-JPA: Create a query by parsing the method name
@Servicepublic class Datainit {    @Autowired    userrepository userrepository;}

However, you will be prompted with an error: Prompt @autowired not inject properly:

Can compile normally, unable to run, Gradle bootrun prompt 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:

In the event of such an error, there are a few questions that need to be checked:

1. Check whether the categories are annotated, including @service, @repository, etc. (note that @autowired is placed on the service implementation, not on the interface class.) )

2. If 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 Bean assembly default rules for Springboot projects are scanned from top to bottom based on the package location where the application class resides !

"Application class" refers to the Springboot project entry class. The location of this class is critical:
If the package for the application class is: Com.boot.app, only the Com.boot.app package and all its child packages will be scanned, and if the service or DAO package is not under Com.boot.app and its child packages, it will not be scanned!
That is, the application class is placed in the DAO, the service is the superior of the package , com.boot.Application

Knowing this is critical, and I don't know if there is any explanation in the spring documentation, and if you don't know it, it can't be solved.

Spring Boot @Autowired not able to automatically inject problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.