手动获取Spring管理的对象

@Component
public class SpringContextBeanUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    @SuppressWarnings("static-access" )
    public void setApplicationContext(ApplicationContext contex)throws BeansException {
        this.context = contex;
    }
    public static Object getBean(String beanName){
        return context.getBean(beanName);
    }
    public static Object getBean(Class beanClass){
        return context.getBean(beanClass);
    }

    public static String getMessage(String key){
        return context.getMessage(key, null, Locale.getDefault());
    }
}

你可能感兴趣的:(java)