取spring容器内bean的工具类
只是记录一下,以后可能可以直接抄来用 单元测试自测OK
package com.mea.pay.transaction.helper; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ApplicationObjectSupport; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; /** * 取spring容器中的bean * * @author Heng.Wei * @date 2022-05-09 **/ @Component public class ApplicationContextUtil extends ApplicationObjectSupport { private static ApplicationContext applicationContext; @PostConstruct void init(){ ApplicationContextUtil.applicationContext = super.getApplicationContext(); } /** * <pre> * 获取springbean * </pre> * * @param beanName bean名称 * @return Bean对象 **/ public static Object getBean(String beanName) { return applicationContext.getBean(beanName); } /** * <pre> * 获取springbean * </pre> * * @param clazz Class类 * @return Bean对象 **/ public static <T> T getBean(Class<T> clazz) { return applicationContext.getBean(clazz); } /** * <pre> * 获取springbean * </pre> * * @param beanName bean名称 * @param clazz Class类 * @return Bean对象 **/ public static <T> T getBean(String beanName, Class<T> clazz) { return applicationContext.getBean(beanName, clazz); } }