zoukankan      html  css  js  c++  java
  • SpringBoot 上下文获取注入的Bean

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    /**
     * 获取Spring上下文
     *
     * @author kelin.ll
     * @date on 2019/7/18
     */
    @Component
    public class ApplicationContextProvider implements ApplicationContextAware {
        /**
         * 上下文对象实例
         */
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.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 <T>
         * @return
         */
        public static <T> T getBean(Class<T> clazz) {
            return getApplicationContext().getBean(clazz);
        }
    
        /**
         * 通过name,以及Clazz返回指定的Bean
         *
         * @param name
         * @param clazz
         * @param <T>
         * @return
         */
        public static <T> T getBean(String name, Class<T> clazz) {
            return getApplicationContext().getBean(name, clazz);
        }
    }

    测试用例:

    import com.provider.ServiceProviderApplication;import lombok.extern.slf4j.Slf4j;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * @author kelin.ll
     * @date on 2019/7/18
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ServiceProviderApplication.class)
    @Slf4j
    public class ApplicationContextProviderTest {
        @Test
        public void getBeanByNameTest(){
            TaskService taskService = (TaskService)ApplicationContextProvider.getBean("taskService");
            System.out.println(taskService.getAll());
        }
    
        @Test
        public void getBeanByClassTest(){
            TaskService taskService = ApplicationContextProvider.getBean(TaskService.class);
            System.out.println(taskService.getAll());
        }
    
        @Test
        public void getBeanByNameAndClassTest(){
            TaskService taskService = ApplicationContextProvider.getBean("taskService",TaskService.class);
            System.out.println(taskService.getAll());
        }
    }
  • 相关阅读:
    [BZOJ3884] 上帝与集合的正确用法
    [BZOJ3518] 点组计数
    [BZOJ3601] 一个人的数论
    [BZOJ3529] [Sdoi2014]数表
    原生js实现无缝滚动轮播图-点击页码即刻显示该页码的内容
    原生js实现无缝滚动轮播图
    vue封装tinymce富文本组件,图片上传回调方法
    vue-cli项目结合Element-ui基于cropper.js封装vue图片裁剪组件
    js实现多文件上传(二)-- 拖拽上传
    js实现多文件上传(一)-- 图片转base64回显
  • 原文地址:https://www.cnblogs.com/gisblogs/p/11207091.html
Copyright © 2011-2022 走看看