Spring boot获取getBean

Posted 也许还年轻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot获取getBean相关的知识,希望对你有一定的参考价值。

package com.job.center.quartz.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author 周志伟
 * @projectname 项目名称: ${project_name}
 * @classname: SpringUtil
 * @description:
 * @date 2018/10/12:16:57
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContextParam) throws BeansException {
        applicationContext=applicationContextParam;
    }
    public static Object getObject(String id) {
        Object object = null;
        object = applicationContext.getBean(id);
        return object;
    }
    public static <T> T getObject(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }

    public static Object getBean(String tClass) {
        return applicationContext.getBean(tClass);
    }

    public <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }
}

 

以上是关于Spring boot获取getBean的主要内容,如果未能解决你的问题,请参考以下文章

spring boot自定义starter

Spring Boot 定时任务单线程和多线程

spring boot 无法读取application.properties问题

Spring boot:thymeleaf 没有正确渲染片段

关于spring获取webApplication.getBean多种途径和简单解释

Spring - 解决 SpringUtil getBean NPE 问题