• 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());
        }
    }
  • 相关阅读:
    php 格利高里函数转化成时间戳
    图片转化成文件流
    下载文件(execl,img 等)
    项目开发流程
    大数据可以采用子查询
    文件上传接口
    多文件ajax 上传
    swoole 安装和简单实用
    jquery 滑动加载
    php实现分享到朋友圈,QQ,微博
  • 原文地址:https://www.cnblogs.com/gisblogs/p/11207091.html
Copyright © 2020-2023  润新知