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. 【NOI2017】泳池

    题解: 满分的笛卡尔树以后再学吧.. 40分还是比较好想的 但是状态挺复杂的 直接贴代码了 代码: #include <bits/stdc++.h> using namespace std ...

  2. Discuz3.2 新用户插入数据库SQL

    我们的网站要和Discuz整合到一起,有个新用户同步的需求,网络上很多的做法是用 UCenter的接口来做,反正最后都是插入SQL,笔者使用了直接操作数据库的方式,把操作的表和SQL整理了下,后面如果 ...

  3. 【AtCoder】ARC075

    ARC075 在省选前一天听说正式选手线画到省二,有了别的女选手,慌的一批,然后刷了一个ARC来稍微找回一点代码感觉 最后还是挂分了,不开心 果然水平退化老年加重啊 原题链接 C - Bugged 直 ...

  4. sparkStreaming消费kafka-1.0.1方式:direct方式(存储offset到Hbase)

    话不多说,可以看上篇博文,关于offset存储到zookeeper https://www.cnblogs.com/niutao/p/10547718.html 本篇博文主要告诉你如何将offset写 ...

  5. HDU2586How far away? LCA

    去博客园看该题解 题意 给出一棵树,以及每条边的权值,给出一些询问,每个询问是2个节点,求每个询问对应的2个节点的距离. 算法 LCA_Tarjan 代码 #include <cstring&g ...

  6. python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor

    python中ThreadPoolExecutor(线程池)与ProcessPoolExecutor(进程池)都是concurrent.futures模块下的,主线程(或进程)中可以获取某一个线程(进 ...

  7. LeetCode 234. 回文链表

    class Solution { public: bool isPalindrome(ListNode* head) { deque<int> d1, d2; ListNode* p = ...

  8. M × N Puzzle POJ - 2893(奇数码)

    The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial inte ...

  9. Nowcoder contest 370F Rinne Loves Edges (简单树形DP) || 【最大流】(模板)

    <题目链接> 题目大意: 一个 $n$ 个节点 $m$ 条边的无向连通图,每条边有一个边权 $w_i$.现在她想玩一个游戏:选取一个 “重要点” S,然后选择性删除一些边,使得原图中所有除 ...

  10. 从香港机房引入google/bitbucket路由

    首先说这种方法有一个局限性:只适用于非CDN网段的引入,比如Google自己足够大,所以用不到CDN,那在香港解析出来的就是Google在香港自己的AS number,因此不存在CDN干扰的问题.如果 ...