[Spring Boot] @Component, @AutoWired and @Primary
Spring boot is really good for Dependencies injection by using Autowiring. Each class instancse in spring boot is called 'Bean', we can use 'Bean' to help us to simplfy the task.
Fro example we have main class:
@SpringBootApplication
public class In28minutesApplication { // What are the beans? --@Component & Bean
// What are the dependencies of a bean? -- @AutoWired
// Where to search for beans => NO NEED public static void main(String[] args) {
// Application Context
ApplicationContext applicationContext =
SpringApplication.run(In28minutesApplication.class, args);
//BinarySearchImpl binarySearch = new BinarySearchImpl(new QuickSortAlgo());
BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class);
int result = binarySearch.binarySearch(new int[] {1,2,3,4}, 3);
System.out.println(result); } }
We can get Bean by using application context and class itself. Now we need to tell Spring boot where to find those Beans, that's by @Component:
@Component
public class BinarySearchImpl { @Autowired
private SortAlgo sortAlgo; public int binarySearch(int [] numbers, int target) {
// Sorting an array sortAlgo.sort(numbers);
System.out.println(sortAlgo);
// Quick sort // Return the result
return 3;
} }
In BinarySearchImpl, we need to autowirte a dependency for 'SortAlgo'.
public interface SortAlgo {
public int[] sort(int[] number);
}
There are two algotihms implements 'SortAlgo' interface:
@Component
@Primary
public class QuickSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
}
@Component
public class BubbleSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
}
Both are marked '@Component', this is important to tell Spring boot, those classes can be autowired. @Primary tell that when multi @Component have the same interface implemented, use @Primary one to autowired.
We can change the logging level to 'debug' in application.properties:
logging.level.org.springframework = debug
Therefore we can see the log:
2019-04-03 13:28:35.502 INFO 6720 --- [ main] c.e.in28minutes.In28minutesApplication : Starting In28minutesApplication on FINPWM10824441 with PID 6720 (C:\Users\z000879\learn\in28minutes\in28minutes\target\classes started by z000879 in C:\Users\z000879\learn\in28minutes)
2019-04-03 13:28:35.503 INFO 6720 --- [ main] c.e.in28minutes.In28minutesApplication : No active profile set, falling back to default profiles: default
2019-04-03 13:28:35.503 DEBUG 6720 --- [ main] o.s.boot.SpringApplication : Loading source class com.example.in28minutes.In28minutesApplication
2019-04-03 13:28:35.573 DEBUG 6720 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/C:/Users/z000879/learn/in28minutes/in28minutes/target/classes/application.properties' (classpath:/application.properties)
2019-04-03 13:28:35.574 DEBUG 6720 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f3b5d16
2019-04-03 13:28:35.590 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2019-04-03 13:28:35.608 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'
2019-04-03 13:28:35.696 DEBUG 6720 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [C:\Users\z000879\learn\in28minutes\in28minutes\target\classes\com\example\in28minutes\BinarySearchImpl.class]
2019-04-03 13:28:35.697 DEBUG 6720 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [C:\Users\z000879\learn\in28minutes\in28minutes\target\classes\com\example\in28minutes\BubbleSortAlgo.class]
2019-04-03 13:28:35.700 DEBUG 6720 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [C:\Users\z000879\learn\in28minutes\in28minutes\target\classes\com\example\in28minutes\QuickSortAlgo.class]
2019-04-03 13:28:35.867 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.condition.BeanTypeRegistry'
2019-04-03 13:28:36.038 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'propertySourcesPlaceholderConfigurer'
2019-04-03 13:28:36.045 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2019-04-03 13:28:36.047 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata'
2019-04-03 13:28:36.048 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2019-04-03 13:28:36.051 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2019-04-03 13:28:36.052 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2019-04-03 13:28:36.056 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor'
2019-04-03 13:28:36.063 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'in28minutesApplication'
2019-04-03 13:28:36.071 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'binarySearchImpl'
2019-04-03 13:28:36.084 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'quickSortAlgo'
2019-04-03 13:28:36.086 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'bubbleSortAlgo'
[Spring Boot] @Component, @AutoWired and @Primary的更多相关文章
- Spring Boot@Component注解下的类无法@Autowired的问题
title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- spring boot开发 @autowired注入失败
@autowired注入失败 会出现如下错误提示: 2018-05-28 08:39:41.857 INFO 8080 --- [ restartedMain] org.hibernate.Versi ...
- spring boot filter -Autowired
需求:在SpringBoot实现拦截器,并且需要自定义的filter类型自动装配一些对象 自定义的过滤器类 public class SessionExpireFilter implements Fi ...
- spring boot(二):注解大全
spring boot注解 @Autowired 注解的意思就是,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去. ...
- Spring Boot 学习之项目构建
最近做了外包,都是工程专业术语,前期熟悉项目看文档看的挺累的,闲暇时间自己学习一下Spring Cloud,找点乐趣. 就有了下面的小项目. 本项目是一个Spring boot项目. 一.nginx做 ...
- Spring Boot + Netty 中 @Autowired, @Value 为空解决
问题描述 使用 Spring Boot + Netty 新建项目时 Handler 中的 @Autowired, @Value 注解的始终为空值 解决方法 @Component // 1. 添加 @C ...
- [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 cla ...
- spring boot 如何将没有注解的类@Autowired
等于将类交给spring管理,也就是IOC. 注解@Autowired是自动装配,也就是spring帮你创建对象,当然前提是这个@Autowired的类已经配置成Bean了,spring配置bean文 ...
随机推荐
- DELPHI - How to use opendialog1 for choosing a folder? TOpenDialog, TFileOpenDialog
DELPHI - How to use opendialog1 for choosing a folder? On Vista and up you can show a more modern lo ...
- Dynamic-Link Library Redirection
Dynamic-Link Library Redirection Applications can depend on a specific version of a shared DLL and s ...
- SQL Server DATEDIFF() 函数(SQL计算时间差)
select * from task_list where 1=1 and datediff(dd,carateTime,getdate()) =0 定义和用法 DATED ...
- setsockopt 设置TCP的选项SO_LINGER
SO_LINGER选项用来设置延迟关闭的时间,等待套接字发送缓冲区中的数据发送完成. 没有设置该选项时,在调用close()后,在发送完FIN后会立即进行一些清理工作并返回.如果设置了SO_LINGE ...
- Windows XP UDF 2.5 补丁,播放蓝光ISO光盘必备
蓝光光盘的文件系统是UDF2.5,Windows XP及以下的操作系统默认不能支持这个文件系统.当我们在XP系统中使用蓝光光盘或蓝光ISO文件时,就会提示“Windows不能从此盘读取,此盘可能已损坏 ...
- Android控件之ImageSwticher
Android控件之ImageSwticher 1. ImageSwticher介绍 ImageSwitcher是图片切换的控件,它能实现图片切换时的动画效果,包括图片导入效果.图片消失效果等等.An ...
- jQuery Ajax 上传文件改进
如果用户取消上传后 背景 提示自动消失了.... 修正Bug.... 同时也更新了不同上传类型的提示字体大小... 2017-05-26 增加了鼠标释放提示 先看之前的效果: 再看现在的效果: 升级 ...
- 都铎王朝第一至四季/全集The Tudors迅雷下载
都铎王朝 第一.二.三.四季 The Tudors Season (2007-2010) 本季看点:本剧讲述了年轻的亨利八世对英国的统治以及他的婚姻生活,带有传奇色彩.Showtime电视台的古装热门 ...
- 疑犯追踪第一季/全集Person Of Interest迅雷下载
本季Person of Interest Season 1 第一季(2011)看点:如今,<疑犯追踪>正在纽约热拍,在11月1日的片场,刚刚完成了一场爆炸的戏.另外,<探索者传说第一 ...
- asp.net mvc5 安装
原文地址 http://docs.nuget.org/docs/start-here/using-the-package-manager-console 工具-->NuGet程序包管理器--&g ...