關(guān)于SpringBoot獲取IOC容器中注入的Bean(推薦)
一: 注入一個(gè)TestUtils類
package com.shop.sell.Utils; import com.shop.sell.dto.CartDTO; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TestUtils { @Bean(name="testDemo") public CartDTO said() { CartDTO cartDTO = new CartDTO(); cartDTO.setProductID(789); cartDTO.setProductQuantity(10); return cartDTO; } }
二: 創(chuàng)建一個(gè)獲取bean的公共類
package com.shop.sell.Utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null) { SpringUtil.applicationContext = applicationContext; } } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name){ return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
三: 在控制器中獲取bean測試結(jié)果
package com.shop.sell.controller; import com.shop.sell.Utils.ResultVOUtil; import com.shop.sell.Utils.SpringUtil; import com.shop.sell.VO.ProductInfoVO; import com.shop.sell.VO.ProductVO; import com.shop.sell.VO.ResultVO; import com.shop.sell.dataobject.ProductCategory; import com.shop.sell.dataobject.ProductInfo; import com.shop.sell.dto.CartDTO; import com.shop.sell.from.OrderForm; import com.shop.sell.service.CategoryService; import com.shop.sell.service.ProductService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 買家商品 */ @RestController @RequestMapping("/buyer/product") public class BuyerProductController { private static ApplicationContext applicationContext; @Autowired private ProductService productService; @Autowired private CategoryService categoryService; @GetMapping(value = "/list") public CartDTO list(){ CartDTO cartDTO = (CartDTO) SpringUtil.getBean("testDemo"); return cartDTO; } }
四: 使用postman測試結(jié)果
總結(jié)
以上所述是小編給大家介紹的關(guān)于SpringBoot獲取IOC容器中注入的Bean(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié)
這篇文章主要介紹了SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09基于java ssm springboot實(shí)現(xiàn)選課推薦交流平臺系統(tǒng)
這篇文章主要介紹了選課推薦交流平臺系統(tǒng)是基于java ssm springboot來的實(shí)現(xiàn)的,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08SpringBoot整合Flyway的方法(數(shù)據(jù)庫版本遷移工具)
這篇文章主要介紹了SpringBoot整合Flyway的方法(數(shù)據(jù)庫版本遷移工具),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06maven項(xiàng)目無法讀取到resource文件夾的問題
這篇文章主要介紹了maven項(xiàng)目無法讀取到resource文件夾的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11解析springBoot-actuator項(xiàng)目構(gòu)造中health端點(diǎn)工作原理
這篇文章主要介紹了springBoot-actuator中health端點(diǎn)工作原理,對spring-boot-actuator的項(xiàng)目構(gòu)造,工作原理進(jìn)行了全面的梳理,側(cè)重health健康檢查部分2022-02-02基于jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理實(shí)現(xiàn)及區(qū)別說明
這篇文章主要介紹了基于jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理實(shí)現(xiàn)及區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05SpringBoot中的ApplicationRunner與CommandLineRunner問題
這篇文章主要介紹了SpringBoot中的ApplicationRunner與CommandLineRunner問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09