[Spring Boot] Use Component Scan to scan for Bean
Component Scan is important concept when we want to create Bean.
Currently we know what, for the class, we want to create Bean from it, we need to add @Component.
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ComponentPersonDAO { @Autowired
ComponentJDBCConnection jdbcConnection; public ComponentJDBCConnection getJdbcConnection() {
return jdbcConnection;
} public void setJdbcConnection(ComponentJDBCConnection jdbcConnection) {
this.jdbcConnection = jdbcConnection;
}
}
Another important thing to know that How Spring Boot knows where to find those @Component, this is where @ComponentScan comes into play. by default, it assume search inside the same package. If inside same package cannot find the Bean, then it will report error.
For example, our main function is inside package called:
com.example.in28minutes
package com.example.in28minutes; import componentScan.ComponentPersonDAO;
... @SpringBootApplication
public class In28minutesComponentScanApplication { private static Logger LOGGER = LoggerFactory.getLogger(In28minutesComponentScanApplication.class); public static void main(String[] args) {
// Application Context
... }
}
But where we import ComponentPersonDAO.java from another package:
componentScan
In this case, the code won't work, it reports that cannot find ComponentPersonDAO bean.
To fix the problem, we can add @ComponentSan("componentScan").
package com.example.in28minutes; import componentScan.ComponentPersonDAO; @SpringBootApplication
@ComponentScan("componentScan")
public class In28minutesComponentScanApplication { ... }
}
It tells the Spring to scan for component inside "componentScan" package.
[Spring Boot] Use Component Scan to scan for Bean的更多相关文章
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...
- Spring Boot 学习系列(09)—自定义Bean的顺序加载
此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Bean 的顺序加载 有些场景中,我们希望编写的Bean能够按照指定的顺序进行加载.比如,有UserServ ...
- 【spring boot】3.spring boot项目,绑定资源文件为bean并使用
整个例子的结构目录如下: 1.自定义一个资源文件 com.sxd.name = 申九日木 com.sxd.secret = ${random.value} com.sxd.intValue = ${r ...
- Spring Boot源码(六):Bean的创建详解
继续之前的项目: People加上无参构造方法: @Component public class People { // private User user; public People(){ Sys ...
- Spring Boot源码(四):Bean装配
为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...
- Spring Boot 自定义配置文件异常"expected single matching bean but found 2"
运行环境:Spring Boot 2.5.0, IDEA 2020.3.2 异常详细信息: Injection of resource dependencies failed; nested exce ...
- 深入Spring Boot:怎样排查expected single matching bean but found 2的异常
写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常. https://github.com/hengy ...
- spring boot注入error,Consider defining a bean of type 'xxx' in your configuration问题解决方案
经常出现这问题一定是非spring生态圈的@标签 没被spring引入,如mybatis等 因为在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动 ...
- Spring boot添加配置类@Configuration并初始化@Bean,@Resource和@Autowired都为null
大写加黑,找了好久@Resource和@Autowired都依赖不到创建的bean的原因:@Bean的方法名即是创建的Bean名称 import org.activiti.engine.Process ...
随机推荐
- STM32 F4 General-purpose Timers for Periodic Interrupts
STM32 F4 General-purpose Timers for Periodic Interrupts
- bitnami下mysql配置-包含phpMyAdmin配置
mysql开启远程访问: 默认情况下mysql的绑定ip是bind-address=127.0.0.1 找到my.cnf bitnami@linux:~$ sudo find / -name my.c ...
- 华为正在力挺的NB-IoT是什么鬼! - 全文
NB-IoT,Niubility Internet of Thing,即牛掰的物联网技术. 关于物联网,小编想从2款很有趣的应用说起. 这不是在播限制级.这是Nake Labs推出的3D健身镜,这款智 ...
- 在ASP.NET MVC中使用Knockout实践06,自定义验证、异步验证
在上一篇中体验了Knockout.Validation的基本验证,本篇体验自定义验证和异步验证. 自定义验证规则 ko.validation有一个rules属性,专门用来存放验证规则,它是一个键值对集 ...
- 11i and R12 Table Count in Different Module
Advertisement Module 11i Tables R12 Tables New Tables AR 551 616 118 BOM 264 337 73 GL 186 309 140 A ...
- 超人前传第一至十季/全集Smallville迅雷下载
超人前传 第一至十季 Smallville Season 1-10 (2001-2010)本季看点:CW台的长寿剧<超人前传>在以帅哥靓女征服观众了这么多年后,也终于进入尾声,该剧将于今年 ...
- Python生成文件列表
https://blog.csdn.net/ZWX2445205419/article/details/73527857 改进 # coding=utf-8 import os def makeFil ...
- SpringMVC request生命周期
When the request leaves the browser, it carries information about what the user is asking for. At ve ...
- “秘书九段的故事”,要学会给自己制定一个工作N段或者技术N段
总经理要求秘书安排次日上午九点开一个会议.在这件事下,什么是任务?什么是结果? 通知到所有参会的人员,然后秘书自己也参加会议来做服务,这是“任务”.但我们想要的结果是什么呢?下面是一至九段秘书的不同做 ...
- 浅谈Hybrid技术的设计与实现【转】
https://www.cnblogs.com/yexiaochai/p/4921635.html 前言 浅谈Hybrid技术的设计与实现 浅谈Hybrid技术的设计与实现第二弹 浅谈Hybrid技术 ...