使用SpringBoot怎么对IOC容器中注入的Bean进行获取

使用SpringBoot怎么对IOC容器中注入的Bean进行获取?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都一家集口碑和实力的网站建设服务商,拥有专业的企业建站团队和靠谱的建站技术,10多年企业及个人网站建设经验 ,为成都数千家客户提供网页设计制作,网站开发,企业网站制作建设等服务,包括成都营销型网站建设,成都品牌网站建设,同时也为不同行业的客户提供成都网站设计、成都做网站的服务,包括成都电商型网站制作建设,装修行业网站制作建设,传统机械行业网站建设,传统农业行业网站制作建设。在成都做网站,选网站制作建设服务商就选成都创新互联公司

一: 注入一个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 getBean(Class clazz){ 
    return getApplicationContext().getBean(clazz); 
  } 
  public static  T getBean(String name,Class 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进行获取的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


本文题目:使用SpringBoot怎么对IOC容器中注入的Bean进行获取
标题网址:http://bzwzjz.com/article/ijocdc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都定制网站建设 网站建设方案 成都响应式网站建设 成都网站建设 成都网站制作公司 成都网站设计 网站建设公司 外贸网站设计方案 高端定制网站设计 成都网站设计制作公司 手机网站制作 四川成都网站制作 网站制作 重庆网站建设 温江网站设计 商城网站建设 成都网站制作 成都做网站建设公司 成都网站建设 高端品牌网站建设 成都商城网站建设 达州网站设计