[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文 ...
 
随机推荐
- [坑] treap
			
先来挖个坑,以后有时间了来补上. treap: 学习资料: fhq式treap http://hi.baidu.com/wdxertqdtscnwze/item/7b6a9419be7c68cd ...
 - GetKeyState(vk_control)
			
GetKeyState(vk_control) 返回负数 , 说明按键被按下了
 - 在ASP.NET Web API中实现CORS(跨域资源共享)
			
默认情况下,是不允许网页从不同的域访问服务器资源的,访问遵循"同源"策略的原则. 会遇到如下的报错: XMLHttpRequest cannot load http://local ...
 - IIS 调用Microsoft.Office.Interop.Word.Documents.Open 返回为null
			
控制面板->管理工具->组件服务->计算机->我的电脑->DCom配置->找到Microsoft Word文档 之后 单击属性打开此应用程序的属性对话框. 2. 单 ...
 - Oracle Applications Documentation
			
Oracle E-Business Suite Documentation Web Library Release 12.2+ Link Download Oracle E-Business Suit ...
 - Android: INSTALL_FAILED_UPDATE_INCOMPATIBLE
			
from://http://xusaomaiss.iteye.com/blog/393296 在反复安装android apk的时候,有的时候可能会遇到adb install错误,内容是:Failur ...
 - 用UICollectionView实现无限轮播图
			
用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节
 - SearchView的最简单的使用方式
			
SearchView顾名思义就是一个搜索视图,和之前讲解的自动匹配的输入框类似.只不过他有自己特有的监听器,并且可以实时得到用户输入的结果.对于SearchView这个控件,我强烈建议将其放在Acti ...
 - 关于Spring-Data-Jpa的一些理解
			
spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管 ...
 - .Net Excel操作之NPOI(一)简介
			
一.NPOI简介 NPOI是一个开源项目,可以读/写xls,doc,ppt文件,有着广泛的应用. 使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支 ...