[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'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-04-03 [GraphQL] Fetch Server Data and Client-side State in One Query using React Apollo + GraphQL
2018-04-03 [Angular] Getting to Know the @Attribute Decorator in Angular
2016-04-03 [Angular 2] Async Http