SpringBoot手动获取bean,调用静态方法从容器中获取对象

重写 ApplicationContextAware 接口里面的 setApplicationContext 方法
使用 @component注解,将普通JavaBean实例化到spring容器中。

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

/**
 * Content:普通类注入
 * @auther: wha
 * @Date: 2019/7/17 9:51
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param 
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param 
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

这样就在其他地方调用静态方法从容器中获取对象了

/**
 * Content:
 *
 * @auther: wha
 * @Date: 2019/11/7 10:27
 */
public class SendMessager {
    private static Logger logger= LoggerFactory.getLogger(SendMessager.class);

    private static RedisService redisService = SpringContextUtils.getBean(RedisServiceImpl.class);

    private static TemplateCodeConfigMapper codeConfigMapper = SpringContextUtils.getBean(TemplateCodeConfigMapper.class);

    private static TemplateCodeConfig getTemplateCodeConfig(Integer id) {
        TemplateCodeConfig templateCodeConfig = codeConfigMapper.selectByPrimaryKey(id);
        return templateCodeConfig;
    }
    public static ReturnData message(){  //静态方法
		TemplateCodeConfig templateCodeConfig = getTemplateCodeConfig(type);
		return ReturnData.success(templateCodeConfig);
   }
}

Spring容器会检测容器中的所有Bean,如果发现某个Bean实现了ApplicationContextAware接口,Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContextAware()方法,调用该方法时,会将容器本身作为参数传给该方法——该方法中的实现部分将Spring传入的参数(容器本身)赋给该类对象的applicationContext实例变量,因此接下来可以通过该applicationContext实例变量来访问容器本身。

你可能感兴趣的:(SpringBoot手动获取bean,调用静态方法从容器中获取对象)