Springboot3.4.x中的@Bean使用案例
前言
Springboot3.4.x
版本中的@Bean新增一個字段defaultCandidate = false
,當(dāng)類型匹配時,基于 Bean 的條件現(xiàn)在將忽略任何不是默認候選者的 Bean
defaultCandidate字段使用
1、 定義一個接口
public interface UserService { void add(); }
2、定義一個接口
@Slf4j public class PersonServiceImpl implements UserService { @Override public void add() { log.info("測試1============="); } }
3、定義一個接口
@Slf4j public class UserServiceImpl implements UserService { @Override public void add() { log.info("測試============="); } }
4、使用@Bean
@Configuration public class UserServiceConfig { @Bean public UserService add() { return new UserServiceImpl(); } @Bean(defaultCandidate = false) public UserService add1() { return new PersonServiceImpl(); } }
5、定義一個接口
@Slf4j @RestController public class IndexController { @Autowired private List<UserService> userServiceList; @GetMapping("/hello") public String hello() { log.info("數(shù)據(jù)為:{{}}", userServiceList); return "success"; } }
訪問地址
http://ip:端口/hello
輸出結(jié)果為
只實例化一個
總結(jié)
Springboot3.4.x中的@Bean中的defaultCandidate = false
,如果存在相同類型的 bean,它就會被忽略
到此這篇關(guān)于Springboot3.4.x中的@Bean使用案例的文章就介紹到這了,更多相關(guān)Springboot3.4.x @Bean使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@Configuration和@Bean和@Component相同點詳解
- SpringBoot中將@Bean方法解析為BeanDefinition詳解
- SpringBoot配置類中@Configuration和@Bean的作用
- SpringBoot在容器中創(chuàng)建實例@Component和@bean有什么區(qū)別
- SpringBoot this調(diào)用@Bean效果詳解
- SpringBoot?@Configuration與@Bean注解使用介紹
- SpringBoot配置@Configuration注解和@bean注解
- Springboot @Configuration @bean注解作用解析
相關(guān)文章
springcloud檢索中間件?ElasticSearch?分布式場景的使用
單機的elasticsearch做數(shù)據(jù)存儲,必然面臨兩個問題:海量數(shù)據(jù)存儲問題、單點故障問題,本文重點給大家介紹springcloud檢索中間件?ElasticSearch?分布式場景的運用,感興趣的朋友跟隨小編一起看看吧2023-10-10SpringCloud中的熔斷監(jiān)控HystrixDashboard和Turbine示例詳解
HystrixDashboard是用于實時監(jiān)控Hystrix性能的工具,展示請求響應(yīng)時間和成功率等數(shù)據(jù),本文介紹了如何配置和使用HystrixDashboard和Turbine進行熔斷監(jiān)控,包括依賴添加、啟動類配置和測試流程,感興趣的朋友一起看看吧2024-09-09SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦
這篇文章主要介紹了SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05