[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文 ...
随机推荐
- 如何调整word中表格某一列占半分比
1.可以拖动,但是不准确 2.
- Revit Family API 添加参数与尺寸标注
使用FamilyManager其他的与普通添加参数与标注没区别. [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Man ...
- DELPHI实现关机,兼容全部WINDOWS系统 转
{=================================================================================================== ...
- .Net Discovery系列之十-深入理解平台机制与性能影响(上)
转眼间<.Net Discovery>系列文章已经推出1年了,本文为该系列的第10-13篇文章,在本文中将对以前所讲的.Net平台知识做一个小小的总结与机制分析,引出并重点介绍这些机制对程 ...
- 移植Python3到TQ2440(一)
平台 硬件:TQ2440 64MB内存 256MB NandFlash bootloader:U-Boot 2015.04 kernel:linux-4.9 Python: Python-3.6.0 ...
- Lombok使用详解(转)
本文转自https://blog.csdn.net/u010695794/article/details/70441432 2017年04月22日 15:17:00 阅读数:10394 Lombok使 ...
- Arcgis Pro为什么我已经安装了汉化包但是显示的还是英文?
- Java Callable和Future简述
创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这两种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果.如果需要获取执行结果,就必须通过共享变量或者使用线程 ...
- GPS定位基本原理浅析
位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...
- Web用户的身份验证及WebApi权限验证流程的设计和实现 asp.net mvc AllowAnonymous 不起作用, asp.net mvc 匿名访问
原文地址: https://blog.csdn.net/zjlovety/article/details/17095627 前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个 ...