Springboot3.4.x中的@Bean使用案例
前言
Springboot3.4.x版本中的@Bean新增一個(gè)字段defaultCandidate = false,當(dāng)類型匹配時(shí),基于 Bean 的條件現(xiàn)在將忽略任何不是默認(rèn)候選者的 Bean
defaultCandidate字段使用
1、 定義一個(gè)接口
public interface UserService {
void add();
}
2、定義一個(gè)接口
@Slf4j
public class PersonServiceImpl implements UserService {
@Override
public void add() {
log.info("測(cè)試1=============");
}
}
3、定義一個(gè)接口
@Slf4j
public class UserServiceImpl implements UserService {
@Override
public void add() {
log.info("測(cè)試=============");
}
}
4、使用@Bean
@Configuration
public class UserServiceConfig {
@Bean
public UserService add() {
return new UserServiceImpl();
}
@Bean(defaultCandidate = false)
public UserService add1() {
return new PersonServiceImpl();
}
}
5、定義一個(gè)接口
@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é)果為

只實(shí)例化一個(gè)
總結(jié)
Springboot3.4.x中的@Bean中的defaultCandidate = false,如果存在相同類型的 bean,它就會(huì)被忽略
到此這篇關(guān)于Springboot3.4.x中的@Bean使用案例的文章就介紹到這了,更多相關(guān)Springboot3.4.x @Bean使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@Configuration和@Bean和@Component相同點(diǎn)詳解
- SpringBoot中將@Bean方法解析為BeanDefinition詳解
- SpringBoot配置類中@Configuration和@Bean的作用
- SpringBoot在容器中創(chuàng)建實(shí)例@Component和@bean有什么區(qū)別
- SpringBoot this調(diào)用@Bean效果詳解
- SpringBoot?@Configuration與@Bean注解使用介紹
- SpringBoot配置@Configuration注解和@bean注解
- Springboot @Configuration @bean注解作用解析
相關(guān)文章
Spring JPA配置文件Eclipse報(bào)錯(cuò)如何解決
這篇文章主要介紹了Spring JPA配置文件Eclipse報(bào)錯(cuò)如何解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的使用
單機(jī)的elasticsearch做數(shù)據(jù)存儲(chǔ),必然面臨兩個(gè)問題:海量數(shù)據(jù)存儲(chǔ)問題、單點(diǎn)故障問題,本文重點(diǎn)給大家介紹springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的運(yùn)用,感興趣的朋友跟隨小編一起看看吧2023-10-10
簡(jiǎn)單的用java實(shí)現(xiàn)讀/寫文本文件的示例
同時(shí)也展示了如果從輸入流中讀出來(lái)內(nèi)容寫入輸出流中(僅限文本流) 三個(gè)例子可以獨(dú)立存在,所以根據(jù)需要只看其中一個(gè)就行了。2008-07-07
Quarkus的Spring擴(kuò)展快速改造Spring項(xiàng)目
這篇文章主要為大家介紹了Quarkus的Spring項(xiàng)目擴(kuò)展,帶大家快速改造Spring項(xiàng)目示例演繹,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
SpringCloud中的熔斷監(jiān)控HystrixDashboard和Turbine示例詳解
HystrixDashboard是用于實(shí)時(shí)監(jiān)控Hystrix性能的工具,展示請(qǐng)求響應(yīng)時(shí)間和成功率等數(shù)據(jù),本文介紹了如何配置和使用HystrixDashboard和Turbine進(jìn)行熔斷監(jiān)控,包括依賴添加、啟動(dòng)類配置和測(cè)試流程,感興趣的朋友一起看看吧2024-09-09
SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦
這篇文章主要介紹了SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

