[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 ... 
随机推荐
- ChibiOS/RT 2.6.9  CAN Driver
			Detailed Description Generic CAN Driver. This module implements a generic CAN (Controller Area Netwo ... 
- 在TQ2440上运行perf,生成Flame Graph
			参考 http://www.cnblogs.com/helloworldtoyou/p/5585152.html http://blog.csdn.net/mtofum/article/detail ... 
- javascript:currentStyle和getComputedStyle的兼容写法
			currentStyle:获取计算后的样式,也叫当前样式.最终样式. 优点:可以获取元素的最终样式,包括浏览器的默认值,而不像style只能获取行间样式,所以更常用到. 注意:不能获取复合样式如bac ... 
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作  完整版+kibana管理ES的index操作
			spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ... 
- go  删除数组元素
			golang中对一个slice进行“slice”可以取片段得到一个新的slice,那么如何用简洁的代码删除slice中的一个元素呢? a := []int{0, 1, 2, 3, 4} //删除第i个 ... 
- Android手机提示“未安装应用程序”
			用eclipse调试应用时,遇到了这个问题,网上给出的解决方案倒是挺多,但似乎一个都没奏效,而且我手机也重启了,还是有问题,郁闷ing- 然后看到一篇文章指出,可能不是签名和SD的卡问题,而是我们 ... 
- 教你摆脱低级程序猿 项目中cocopads的安装使用
			小农今天聊聊一款作为iOS开发者必备的第三方管理软件.希望程序猿朋友们看到小农的这篇文章后.可以真正的学会怎样灵活管理你项目中的第三方. (一)CocoaPods是什么? 首先我们来认识一下这款第三方 ... 
- Android获取actionbar高度和StatusBar高度的方法
			ActionBar: getActionBar().getHeight(); StatusBar: /** * 获取状态栏高度 * * @return */ public static int get ... 
- [Web 前端]   CSS篇之3. 如何保持浮层水平垂直居中
			原文链接](http://www.cnblogs.com/yaliu/p/5190957.html) 浮层水平垂直居中方法 (一)利用绝对定位与transform <div class=&quo ... 
- Java和C#差异点
			语法:----------------------------------------------------------1. Java的byte为-128~127相当于c#的sbyte,c#byte ... 
