您好,登錄后才能下訂單哦!
使用SpringBoot怎么對(duì)IOC容器中注入的Bean進(jìn)行獲?。肯嘈藕芏鄾]有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
一: 注入一個(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測(cè)試結(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; } }
看完上述內(nèi)容,你們掌握使用SpringBoot怎么對(duì)IOC容器中注入的Bean進(jìn)行獲取的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。