• 使用spring注解时@Service注解,在注入时为null的问题


    1、上工具类(使用上下文)

    
    @Slf4j
    @Component
    public class SpringUtil implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        // 获取applicationContext
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (SpringUtil.applicationContext == null) {
                SpringUtil.applicationContext = applicationContext;
            }
            log.debug("获取ApplicationContext");
        }
    
        // 通过name获取Bean
        public static Object getBean(String name) {
            return getApplicationContext().getBean(name);
        }
    
        // 通过class获取Bean
        public static <T> T getBean(Class<T> clazz) {
            return getApplicationContext().getBean(clazz);
        }
    
        // 通过name,以及Clazz返回指定的Bean
        public static <T> T getBean(String name, Class<T> clazz) {
            return getApplicationContext().getBean(name, clazz);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2、使用方法

     private WorkSignBusinessService initWorkFlowBusinessService(String tableName) {
            WorkSignBusinessService workSignBusinessService = null;
            switch (tableName) {
                case "t_agent_info":
                    workSignBusinessService = workSignBusinessServiceAgentInfo == null ? SpringUtil.getBean(WorkSignBusinessServiceAgentInfo.class) : workSignBusinessServiceAgentInfo;
                    break;
                case "t_applicant_info":
                    workSignBusinessService = workSignBusinessServiceApplicantInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceApplicantInfo.class) : workSignBusinessServiceApplicantInfo;
                    break;
                case "t_case_handle":
                    workSignBusinessService = workSignBusinessServiceCaseHandle== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseHandle.class) : workSignBusinessServiceCaseHandle;
                    break;
                case "t_case_handle_record":
                    workSignBusinessService = workSignBusinessServiceCaseHandleRecord== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseHandleRecord.class) : workSignBusinessServiceCaseHandleRecord;
                    break;
                case "t_case_handle_user":
                    workSignBusinessService = workSignBusinessServiceCaseHandleUser== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseHandleUser.class) : workSignBusinessServiceCaseHandleUser;
                    break;
                case "t_case_info":
                    workSignBusinessService = workSignBusinessServiceCaseInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseInfo.class) : workSignBusinessServiceCaseInfo;
                    break;
                case "t_case_modify_record":
                    workSignBusinessService = workSignBusinessServiceCaseModifyRecord== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseModifyRecord.class) : workSignBusinessServiceCaseModifyRecord;
                    break;
                case "t_case_threerule":
                    workSignBusinessService = workSignBusinessServiceCaseThreeRule== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseThreeRule.class) : workSignBusinessServiceCaseThreeRule;
                    break;
                case "t_case_work_extend":
                    workSignBusinessService = workSignBusinessServiceCaseWorkExtend== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseWorkExtend.class) : workSignBusinessServiceCaseWorkExtend;
                    break;
                case "t_case_work_rl":
                    workSignBusinessService = workSignBusinessServiceCaseWorkRl== null ? SpringUtil.getBean(WorkSignBusinessServiceCaseWorkRl.class) : workSignBusinessServiceCaseWorkRl;
                    break;
                case "t_deliver_receiver":
                    workSignBusinessService = workSignBusinessServiceDeliverReceiver== null ? SpringUtil.getBean(WorkSignBusinessServiceDeliverReceiver.class) : workSignBusinessServiceDeliverReceiver;
                    break;
                case "t_handle_user_record":
                    workSignBusinessService = workSignBusinessServiceHandleUserRecord== null ? SpringUtil.getBean(WorkSignBusinessServiceHandleUserRecord.class) : workSignBusinessServiceHandleUserRecord;
                    break;
                case "t_overview_back_record":
                    workSignBusinessService = workSignBusinessServiceOverViewBackRecord== null ? SpringUtil.getBean(WorkSignBusinessServiceOverViewBackRecord.class) : workSignBusinessServiceOverViewBackRecord;
                    break;
                case "t_proxy_info":
                    workSignBusinessService = workSignBusinessServiceProxyInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceProxyInfo.class) : workSignBusinessServiceProxyInfo;
                    break;
                case "t_respondent_info":
                    workSignBusinessService = workSignBusinessServiceRespondentInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceRespondentInfo.class) : workSignBusinessServiceRespondentInfo;
                    break;
                case "t_sms_record":
                    workSignBusinessService = workSignBusinessServiceSmsRecord== null ? SpringUtil.getBean(WorkSignBusinessServiceSmsRecord.class) : workSignBusinessServiceSmsRecord;
                    break;
                case "t_sys_config":
                    workSignBusinessService = workSignBusinessServiceSysConfig== null ? SpringUtil.getBean(WorkSignBusinessServiceSysConfig.class) : workSignBusinessServiceSysConfig;
                    break;
                case "t_third_person_info":
                    workSignBusinessService = workSignBusinessServiceThirdPersonInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceThirdPersonInfo.class) : workSignBusinessServiceThirdPersonInfo;
                    break;
                case "t_ws_result":
                    workSignBusinessService = workSignBusinessServiceWsResult== null ? SpringUtil.getBean(WorkSignBusinessServiceWsResult.class) : workSignBusinessServiceWsResult;
                    break;
                case "t_ws_temp":
                    workSignBusinessService = workSignBusinessServiceWsTemp== null ? SpringUtil.getBean(WorkSignBusinessServiceWsTemp.class) : workSignBusinessServiceWsTemp;
                    break;
                case "t_uap_app":
                    workSignBusinessService = workSignBusinessServiceUapApp== null ? SpringUtil.getBean(WorkSignBusinessServiceUapApp.class) : workSignBusinessServiceUapApp;
                    break;
                case "t_uap_group":
                    workSignBusinessService = workSignBusinessServiceUapGroup== null ? SpringUtil.getBean(WorkSignBusinessServiceUapGroup.class) : workSignBusinessServiceUapGroup;
                    break;
                case "t_uap_user":
                    workSignBusinessService = workSignBusinessServiceUapUser== null ? SpringUtil.getBean(WorkSignBusinessServiceUapUser.class) : workSignBusinessServiceUapUser;
                    break;
                case "t_entrusting_depart":
                    workSignBusinessService = workSignBusinessServiceEntrustingDepart== null ? SpringUtil.getBean(WorkSignBusinessServiceEntrustingDepart.class) : workSignBusinessServiceEntrustingDepart;
                    break;
                case "t_wssq_applicant_info":
                    workSignBusinessService = workSignBusinessServiceWssqApplicantInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceWssqApplicantInfo.class) : workSignBusinessServiceWssqApplicantInfo;
                    break;
                case "t_wssq_evidence":
                    workSignBusinessService = workSignBusinessServiceWssqEvidence== null ? SpringUtil.getBean(WorkSignBusinessServiceWssqEvidence.class) : workSignBusinessServiceWssqEvidence;
                    break;
                case "t_wssq_inquire":
                    workSignBusinessService = workSignBusinessServiceWssqInquire== null ? SpringUtil.getBean(WorkSignBusinessServiceWssqInquire.class) : workSignBusinessServiceWssqInquire;
                    break;
                case "t_wssq_login":
                    workSignBusinessService = workSignBusinessServiceWssqLogin== null ? SpringUtil.getBean(WorkSignBusinessServiceWssqLogin.class) : workSignBusinessServiceWssqLogin;
                    break;
                case "t_wssq_overt":
                    workSignBusinessService = workSignBusinessServiceWssqOvert== null ? SpringUtil.getBean(WorkSignBusinessServiceWssqOvert.class) : workSignBusinessServiceWssqOvert;
                    break;
                case "t_wssq_respondent_info":
                    workSignBusinessService = workSignBusinessServiceWssqRespondentInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceWssqRespondentInfo.class) : workSignBusinessServiceWssqRespondentInfo;
                    break;
                case "t_person_info":
                    workSignBusinessService = workSignBusinessServicePersonInfo== null ? SpringUtil.getBean(WorkSignBusinessServicePersonInfo.class) : workSignBusinessServicePersonInfo;
                    break;
                case "xssht_respondent_info":
                    workSignBusinessService = workSignBusinessServiceXsshRespondentInfo== null ? SpringUtil.getBean(WorkSignBusinessServiceXsshRespondentInfo.class) : workSignBusinessServiceXsshRespondentInfo;
                    break;
                case "ed_user":
                    workSignBusinessService = workSignBusinessServiceEdUser== null ? SpringUtil.getBean(WorkSignBusinessServiceEdUser.class) : workSignBusinessServiceEdUser;
                    break;
    
            }
            return workSignBusinessService;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
  • 相关阅读:
    elementui上传图片,到达最大数量隐藏上传按钮,判断文件格式是不是png jpg,文件最大5m
    ssm项目环境初步搭建
    Electron集成React和Vue
    java进行系统的限流实现--Guava RateLimiter、简单计数、滑窗计数、信号量、令牌桶
    nginx-配置拆分(各个模块详细说明)
    如何使用 Hotshot 通过文字生成 GIF 动画
    【LeetCode热题100】【多维动态规划】编辑距离
    【AI实用技巧】GPT写sql统计语句
    MySQL事务与MVCC如何实现的隔离级别
    触摸屏如何利用无线PPI通信模块远程采集PLC数据?
  • 原文地址:https://blog.csdn.net/zouyang920/article/details/127719477