国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

關于SpringBoot獲取IOC容器中注入的Bean(推薦)

2024-07-14 08:40:49
字體:
來源:轉載
供稿:網友

一: 注入一個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;   } } 

    二: 創建一個獲取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測試結果

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測試結果

spring,boot,ioc容器,bean

總結

以上所述是小編給大家介紹的關于SpringBoot獲取IOC容器中注入的Bean(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜川县| 靖州| 饶河县| 游戏| 锡林郭勒盟| 灵寿县| 天门市| 海原县| 汝州市| 遂溪县| 遂平县| 陆良县| 云梦县| 武汉市| 平陆县| 河南省| 文成县| 和田市| 庆云县| 清原| 桃源县| 游戏| 普安县| 太原市| 黄山市| 临澧县| 民权县| 通许县| 济阳县| 珲春市| 府谷县| 江西省| 盐边县| 鄯善县| 仁寿县| 绥芬河市| 新野县| 秭归县| 贵港市| 普格县| 姚安县|