使用SpringBoot怎么對IOC容器中注入的Bean進行獲取

使用SpringBoot怎么對IOC容器中注入的Bean進行獲取?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都一家集口碑和實力的網站建設服務商,擁有專業(yè)的企業(yè)建站團隊和靠譜的建站技術,10多年企業(yè)及個人網站建設經驗 ,為成都數(shù)千家客戶提供網頁設計制作,網站開發(fā),企業(yè)網站制作建設等服務,包括成都營銷型網站建設,成都品牌網站建設,同時也為不同行業(yè)的客戶提供成都網站設計、成都做網站的服務,包括成都電商型網站制作建設,裝修行業(yè)網站制作建設,傳統(tǒng)機械行業(yè)網站建設,傳統(tǒng)農業(yè)行業(yè)網站制作建設。在成都做網站,選網站制作建設服務商就選成都創(chuàng)新互聯(lián)公司。

一: 注入一個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)建一個獲取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; 
  } 
}

看完上述內容,你們掌握使用SpringBoot怎么對IOC容器中注入的Bean進行獲取的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當前文章:使用SpringBoot怎么對IOC容器中注入的Bean進行獲取
標題路徑:http://muchs.cn/article12/ijocdc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、電子商務、手機網站建設網站改版、標簽優(yōu)化、App開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網站優(yōu)化