欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于SpringBoot获取IOC容器中注入的Bean(推荐)

程序员文章站 2023-12-13 09:49:58
一: 注入一个testutils类 package com.shop.sell.utils; import com.shop.sell.dto.cartdto...

一: 注入一个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(推荐)

总结

以上所述是小编给大家介绍的关于springboot获取ioc容器中注入的bean(推荐),希望对大家有所帮助

上一篇:

下一篇: