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

时间:2022-09-18 16:02:53

一: 注入一个testutils类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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的公共类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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测试结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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(推荐),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/weixin_42021727/article/details/80318864