Application 启动类:

@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("成功启动");
}

Dao层:

public interface UserRepository extends JpaRepository<User, String>{
User findByUsername(String username); //
}
Service 层:注入一个继承了JPA 的接口,理论上spring boot 会把JPA 注入repository,该接口是一定不需要实现类的.
上述方法实现了按Username查询User实体,可以看到我们这里没有任何类SQL语句就完成了个条件查询方法。
这就是Spring-data-jpa的一大特性:通过解析方法名创建查询

@Service
public class DataInit { @Autowired
UserRepository userRepository;
}

但是会提示错误:提示@Autowired 不能正常注入:

可正常编译,没法运行,gradle bootrun 时提示错误:

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

总结:

出现该类错误时,可有一下几个问题需要去检查:

1. 检查各类是否加了注解,包括@service,@repository 等等;(注意@Autowired放在service实现上,而不是接口类上面。)

2. 包是否正确扫描到,这个很重要!!!(我的问题就是因为这个)

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

SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!

“Application类”是指SpringBoot项目入口类。这个类的位置很关键:
如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描!
即, 把Application类放到dao、service所在包的上级,com.boot.Application

知道这一点非常关键,不知道spring文档里有没有给出说明,如果不知道还真是无从解决.

Spring Boot @Autowired 没法自动注入的问题的更多相关文章

  1. spring boot测试类自动注入service或dao

    使用Spring Boot进行单元测试时,发现使用@Autowired注解的类无法自动注入,当使用这个类的实例的时候,报出NullPointerException,即空指针异常. Spring Boo ...

  2. Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean

    在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...

  3. Quartz与Spring集成 Job如何自动注入Spring容器托管的对象

    在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类和方法可以是普通类.很显然,第二种方式远比第一种方式来的 ...

  4. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

  5. Spring Boot之配置文件值注入(@ConfigurationProperties)

    前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...

  6. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  7. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  8. Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

  9. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

随机推荐

  1. sendfile

    Sendfile 函数在两个文件描写叙述符之间直接传递数据(全然在内核中操作,传送),从而避免了内核缓冲区数据和用户缓冲区数据之间的拷贝,操作效率非常高,被称之为零拷贝. Sendfile 函数的定义 ...

  2. JQ JS复制到剪贴板

    示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  3. BZOJ4589 Hard Nim FWT 快速幂 博弈

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4589.html 题目传送门 - BZOJ4589 题意 有 $n$ 堆石子,每一堆石子的取值为 $2$ ...

  4. windows安装Python虚拟环境

    1.pip install virtualenv 安装virtualenv镜像 3.virtualenv  scrapytest 新建virtualenv 4.cd到\scrapytest\Scrip ...

  5. SSM整合——完全版

    1, 2, 3, 4,项目建立好后: 覆盖pom.xml,地址在:https://blog.csdn.net/mark_lirenhe/article/details/80875266 alt+F5= ...

  6. 工具Maven

    Maven 1.使用Maven后每个jar包只在本地仓库中保存一份,需要jar包的工程只需要维护一个文本形式的jar包的引用——我们称之为“坐标”. 2.Maven就可以替我们自动的将当前jar包所依 ...

  7. 实现判断条件中有in的判断

    如果是简单的写sql,在where中写死就可以了,但是如果是不确定的参数呢,这个时候就需要一点处理方式了. 1.后台的写法 String[] operateResult=new String[]{&q ...

  8. 050 Kafka的引入介绍

    高吞吐量的分布式订阅消息系统 1.官网 http://kafka.apache.org/ 2.官网的介绍 3.结构 这个是版本1.0之后的版本. In Kafka the communication ...

  9. day 48-css-part1

    CSS(Cascading Style Sheet,层叠样式表 css是前端的优化器,如果说我们的html是把前端的大体骨架搭起来的话,那么我们的css就是在这个骨架的基础上进行修饰,使之更有立体感, ...

  10. Codeforces 1105C Ayoub and Lost Array (计数DP)

    <题目链接> 题目大意: 有一个长度为 n 的数列的未知数列,数列的每一个数的值都在区间 [l,r]  的范围内.现在问你能够构成多少个这样的数组,使得数组内的所有数的和能够被 3 整除. ...