介绍
使用SpringBoot怎么对国际奥委会容器中注入的Bean进行获取?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<强>一:注入一个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=皌estDemo"), public 才能;CartDTO 说(),{, ,,,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 {, ,,,如果(SpringUtil.applicationContext ==, null), {, ,,,,,SpringUtil.applicationContext =, applicationContext;, ,,,}, ,,}, public 才能static ApplicationContext  getApplicationContext (), {, ,,,return applicationContext;, ,,}, public 才能static Object  getBean (String 名称){, ,,,return getApplicationContext () .getBean(名字),, ,,}, public 才能static & lt; T>, T getBean (Class, T getBean (String 名字,Class <强>三:在控制器中获取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 并不知道,/* *, ,*买家商品, ,*/@RestController @RequestMapping(“/买家/product"), {public class  BuyerProductController private 才能static ApplicationContext  applicationContext;, @Autowired 才能; private 才能;ProductService  productService;, @Autowired 才能; private 才能;CategoryService  categoryService;, @GetMapping才能(value =,“/list"), public 才能;CartDTO 列表(){, ,,,CartDTO CartDTO =, (CartDTO), SpringUtil.getBean (“testDemo");, ,,,return cartDTO;, ,,}, }看完上述内容,你们掌握使用SpringBoot怎么对国际奥委会容器中注入的Bean进行获取的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
使用SpringBoot怎么对国际奥委会容器中注入的Bean进行获取