惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
F
Fortinet All Blogs
B
Blog
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
The Register - Security
The Register - Security
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
B
Blog RSS Feed
WordPress大学
WordPress大学
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
O
OpenAI News
月光博客
月光博客
H
Hacker News: Front Page
S
Security Affairs
W
WeLiveSecurity
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
A
About on SuperTechFans

博客园 - PointNet

log4j2发送消息至Kafka log4j2自定义Appender(输出到文件/RPC服务中) log4j 不同模块输出到不同的文件 shell之磁盘容量检查,配合crontab可以定时清理磁盘 spring注解之@profile How to do conditional auto-wiring in Spring? MyBatis传入参数为集合 list 数组 map写法 mysql5.5 uuid做主键与int做主键的性能实测 dom4j解析xml字符串实例 spring自动注入是单例还是多例?单例如何注入多例? Spring中Bean的五个作用域 【总结】瞬时高并发(秒杀/活动)Redis方案 浅谈分布式事务 基于Redis实现分布式锁 MySQL事务隔离级别详解 Redis学习手册(Sorted-Sets数据类型) Redis的快照持久化-RDB与AOF Redis - 事务 ArrayList、Vector、HashMap、HashTable、HashSet的默认初始容量、加载因子、扩容增量
Spring Boot @Autowired 没法自动注入的问题
PointNet · 2017-10-10 · via 博客园 - PointNet

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文档里有没有给出说明,如果不知道还真是无从解决.