[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 ...
随机推荐
- rcp(插件开发)插件B需要引用插件A中的jar包-如何处理依赖关系
如果插件B需要引用插件A中的jar 通常需要以下几步: 1.插件B要依赖插件A 2.在插件B的build path中添加插件A的jar包 3.插件A的runtime导出插件B中使用jar的packag ...
- Android记录3--ExpandableListView使用+获取SIM卡状态信息
Android记录3--ExpandableListView使用+获取SIM卡状态信息 2013年8月9日Android记录 ExpandableListView是一个可以实现下拉列表的控件,大家可能 ...
- Revit Family API 添加几何实体
先创建一个封闭曲线createProfileLShape();再创建实体,这里需要手工画一个参考平面; ; i < nVerts; ++i) { Line l ...
- 基于设备树的TQ2440的中断(2)
下面以按键中断为例看看基于设备数的中断的用法: 设备树: tq2440_key { compatible = "tq2440,key"; interrupt-parent = &l ...
- AngularJS中实现显示或隐藏动画效果的3种方式
本篇体验在AngularJS中实现在"显示/隐藏"这2种状态切换间添加动画效果. 通过CSS方式实现显示/隐藏动画效果 思路: →npm install angular-anima ...
- js中 object.constructor
- Windows Phone本地数据库(SQLCE):4、[Column]attribute(翻译) (转)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第四篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...
- Objective-C中的一些特殊的数据类及NSLog的输出格式
NSLog的格式如下所示: %@ 对象 %d, %i 整数 %u 无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t % ...
- pthread_join与pthread_detach细节问题
http://www.360doc.com/content/13/0106/09/9171956_258497083.shtml pthread_t pthr; pthread_create(& ...
- centos7安装debuginfo
转自:https://www.72zk.com/show/blog/20 查看内核版本,查找对应的内核rpm文件 [root@localhost ~]#uname -rsp Linux 3.10.0- ...