[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 ...
随机推荐
- HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)
Minimum palindrome Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 反接保护电路 Reverse Voltage Protection
Reverse Voltage Protection I've long wanted to pull together some reverse polarity protection ideas ...
- Golang 处理 Json(一):编码
JSON 是一种数据格式描述语言.以 key 和 value 构成的哈系结构,类似 Javascript 中的对象,python 中的字典.通常 json 格式的 key 是字符串,其值可以是任意类型 ...
- Android 应用程序之间内容分享详解(一)
一个Andoird应用程序的重要的地方是他们有相互沟通和整合的能力,一个应用程序可以和另一个应用程序交互,接下来我们来看看Android应用之间的内容分享 当你构建Intent的时候,必须要指定Int ...
- Hadoop: the definitive guide 第三版 拾遗 第四章
第四章中提到了通过CompressionCodec对streams进行压缩和解压缩,并提供了示例程序: 输入:标准输入流 输出:压缩后的标准输出流 // cc StreamCompressor A p ...
- Revit Family API 添加几何实体
先创建一个封闭曲线createProfileLShape();再创建实体,这里需要手工画一个参考平面; ; i < nVerts; ++i) { Line l ...
- EditPlus(4.0.0.395)中文免激活绿色版
EditPlus一套功能强大,可取代记事本的文字编辑器,拥有无限制的撤消与重做.英文拼字检查.自动换行.列数标记.搜寻取代.同时编辑多文件.全屏幕浏览功能.而它还有一个好用的功能,就是它有监视剪贴板的 ...
- ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置
ASP.NET Web API的路由和ASP.NET MVC相似,也是把路由放在RouteTable中的.可以在App_Start文件夹中的WebApiConfig.cs中设置路由模版.默认的路由模版 ...
- delphi 合并两个 Wav 文件流的函数
合并两个 Wav 文件的函数 实例一 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphic ...
- 简述 IOS中的LazyLoad思想
Lazy,谁懒?当然是计算机偷懒.通常用法,你有一个NSArray的property,但是你不在初始化方法里为其alloc/init,它就只是一个指针,不会占用内存.然后你写了此property的访问 ...