关于SpringBoot获取IOC容器中注入的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="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测试结果

总结

以上所述是小编给大家介绍的关于SpringBoot获取IOC容器中注入的Bean(推荐),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。