服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 关于SpringBoot获取IOC容器中注入的Bean(推荐)

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

2021-04-29 10:56noodles1994 Java教程

本文通过实例代码给大家详解了springboot获取ioc容器中注入的bean问题,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧

一: 注入一个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

延伸 · 阅读

精彩推荐