关于SpringBoot获取IOC容器中注入的Bean

本文介绍了一个Spring Boot项目的示例,展示了如何定义并注入一个CartDTO类型的Bean到TestUtils类,接着创建了一个公共类SpringUtil来方便地从Spring上下文中获取Bean实例。最后,在BuyerProductController控制器中演示了如何通过SpringUtil获取名为testDemo的Bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    一: 注入一个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测试结果



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值