电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

普通類注入不進spring bean的解決方法

瀏覽:12日期:2023-07-24 10:56:04

解決問題:我在做移動端accessToken的使用遇到一個問題,就是普通類死活注入不進去spring bean,我和同事雷杰通過各種注解,xml配置搞了好久都搞不定,這里插個眼,有空補一下spring,得深入研究一下

解決辦法:后面通過一個spring工具類搞定,這里貼上代碼

1、引入這個springUtil類

普通類注入不進spring bean的解決方法

2、通過構造方法注入

普通類注入不進spring bean的解決方法

貼上SpringUtils代碼:

package com.dt.base.weixin.util;import org.springframework.aop.framework.AopContext;import org.springframework.beans.BeansException;import org.springframework.beans.factory.NoSuchBeanDefinitionException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.stereotype.Component;/** * @Description: spring工具類 方便在非spring管理環境中獲取bean * @author: ZhangChongHu * @Date: 2020/12/8 17:23 * @Copyright: Xi’an Dian Tong Software Co., Ltd. All Rights Reserved. * @Version 1.0 */@Componentpublic final class SpringUtils implements BeanFactoryPostProcessor{ /** Spring應用上下文環境 */ private static ConfigurableListableBeanFactory beanFactory; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtils.beanFactory = beanFactory; } /** * 獲取對象 * * @param name * @return Object 一個以所給名字注冊的bean的實例 * @throws BeansException * */ @SuppressWarnings('unchecked') public static <T> T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } /** * 獲取類型為requiredType的對象 * * @param clz * @return * @throws BeansException * */ public static <T> T getBean(Class<T> clz) throws BeansException { T result = (T) beanFactory.getBean(clz); return result; } /** * 如果BeanFactory包含一個與所給名稱匹配的bean定義,則返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return beanFactory.containsBean(name); } /** * 判斷以給定名字注冊的bean定義是一個singleton還是一個prototype。 如果與給定名字相應的bean定義沒有被找到,將會拋出一個異常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException * */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return beanFactory.isSingleton(name); } /** * @param name * @return Class 注冊對象的類型 * @throws NoSuchBeanDefinitionException * */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return beanFactory.getType(name); } /** * 如果給定的bean名字在bean定義中有別名,則返回這些別名 * * @param name * @return * @throws NoSuchBeanDefinitionException * */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return beanFactory.getAliases(name); } /** * 獲取aop代理對象 * * @param invoker * @return */ @SuppressWarnings('unchecked') public static <T> T getAopProxy(T invoker) { return (T) AopContext.currentProxy(); }}

貼上調用得方法:

注意:調用getValidator()方法直接返回得是 AgentCfgDao agentCfgDao ,相當于

@Autowired private AgentCfgDao agentCfgDao;

/** * Copyright (c) 2014 - 2016 Xi’an Dian Tong Software Co., Ltd. All Rights Reserved. * <p> * This software is the confidential and proprietary information of Xi’an Dian Tong * Software Co., Ltd. ('Confidential Information'). You shall not disclose such * Confidential Information and shall use it only in accordance with the terms * of the license agreement you entered into with Xi’an Dian Tong Software Co., Ltd. */package com.dt.base.weixin.app;import cn.hutool.http.HttpRequest;import cn.hutool.http.HttpUtil;import com.dt.base.weixin.util.SpringUtils;import com.dt.ncfg.dao.AgentCfgDao;import com.dt.sys.manage.entity.DtwxAgentCfg;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.stereotype.Component;import java.util.HashMap;/** * 保存了 corpID + secret 和對應的 access token 。 * key: corpID + secret * value: access token */public class AccessTokenPool { protected final static Logger log = LogManager.getLogger('AccessTokenPool'); DtwxAgentCfg dtwxAgentCfg = null; /** * 獲取AgentCfgDao * * @return */ protected AgentCfgDao getValidator() { return SpringUtils.getBean(AgentCfgDao.class); } /** * 根據corpID, secret 換取AccessToken * * @param corpID corpID * @param secret secret * @param type type * @return */ public String getAccessToken(String corpID, String secret, String type) { //如果是企業號 if ('QYH'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(); paramMap.put('corpId', corpID); paramMap.put('corpSecret', secret); String result = HttpUtil.get(resUrl() + '/api/mobile/QYH/isExist', paramMap); return result; } //如果是服務號 if ('FWH'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(); paramMap.put('appId', corpID); paramMap.put('appSecret', secret); String result = HttpUtil.get(resUrl() + '/api/mobile/FWH/isExist', paramMap); return result; } //如果是釘釘號 if ('DING'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(); paramMap.put('appKey', corpID); paramMap.put('appSecret', secret); String result = HttpUtil.get(resUrl() + '/api/mobile/DING/isExist', paramMap); return result; } return null; } /** * 根據corpID, secret 刪除舊的token * * @param corpID * @param secret * @return */ public String delAccessToken(String corpID, String secret, String type) { if ('QYH'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('corpId', corpID); paramMap.put('corpSecret', secret); //請求微服務接口地址 HttpRequest.delete(resUrl() + '/api/mobile/QYH') .form(paramMap).execute().body(); return null; } if ('FWH'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('appId', corpID); paramMap.put('appSecret', secret); //請求微服務接口地址 HttpRequest.delete(resUrl() + '/api/mobile/FWH') .form(paramMap).execute().body(); return null; } if ('DING'.equals(type)) { HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('appKey', corpID); paramMap.put('appSecret', secret); //請求微服務接口地址 HttpRequest.delete(resUrl() + '/api/mobile/DING') .form(paramMap).execute().body(); return ''; } return ''; } /** * 根據corpID, secret 換取JSTicket * * @param corpID * @param secret * @return */ public String getJSTicket(String corpID, String secret, String type) { if ('QYH'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('corpId', corpID); paramMap.put('corpSecret', secret); //請求微服務接口地址 String result = HttpUtil.get(resUrl() + '/api/mobile/QYH/isJSTicket', paramMap); return result; } if ('FWH'.equals(type)) { //可以單獨傳入http參數,這樣參數會自動做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('appId', corpID); paramMap.put('appSecret', secret); //請求微服務接口地址 String result = HttpUtil.get(resUrl() + '/api/mobile/FWH/isJSTicket', paramMap); return result; } if ('DING'.equals(type)) { HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('appKey', corpID); paramMap.put('appSecret', secret); //請求微服務接口地址 String result = HttpUtil.get(resUrl() + '/api/mobile/DING/isJSTicket', paramMap); return result; } return ''; } /** * 獲取數據庫中的url * @return url 地址 */ public String resUrl(){ //獲取url DtwxAgentCfg dtwxAgentCfg = new DtwxAgentCfg(); dtwxAgentCfg.setAppType('wxInterfaceUrl'); dtwxAgentCfg.setConfigKey('RESQUEST_ACS_TOKEN'); DtwxAgentCfg agentCfg = getValidator().selectDataCfg(dtwxAgentCfg); //url=http://localhost:8080 String url = agentCfg.getConfigValue(); return url; }}

總結:bug是搞定了,但是基礎知識還要補,打卡現在是2020/12/16寫得博客,那天把這里得知識補了,在回來留痕。

以上就是普通類注入不進spring bean的解決方法的詳細內容,更多關于普通類注入不進spring bean的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 喷涂流水线,涂装流水线,喷漆流水线-山东天意设备科技有限公司 | 螺杆泵_中成泵业| 灌木树苗-绿化苗木-常绿乔木-价格/批发/基地 - 四川成都途美园林 | 西门子代理商_西门子变频器总代理-翰粤百科 | 一路商机网-品牌招商加盟优选平台-加盟店排行榜平台 | 泰州物流公司_泰州货运公司_泰州物流专线-东鑫物流公司 | 消防设施操作员考试报名时间,报名入口,报考条件 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 衢州装饰公司|装潢公司|办公楼装修|排屋装修|别墅装修-衢州佳盛装饰 | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | 底部填充胶_电子封装胶_芯片封装胶_芯片底部填充胶厂家-东莞汉思新材料 | 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 有声小说,听书,听小说资源库-听世界网 | 闪蒸干燥机-喷雾干燥机-带式干燥机-桨叶干燥机-[常州佳一干燥设备] | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 | 云南外加剂,云南速凝剂,云南外加剂代加工-普洱澜湄新材料科技有限公司 | 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 无菌水质袋-NASCO食品无菌袋-Whirl-Pak无菌采样袋-深圳市慧普德贸易有限公司 | 宿舍管理系统_智慧园区系统_房屋/房产管理系统_公寓管理系统 | 保温杯,儿童婴童奶瓶,运动水壶「广告礼品杯定制厂家」超朗保温杯壶 | 深圳成考网-深圳成人高考报名网 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 家乐事净水器官网-净水器厂家「官方」| lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程| 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 钢结构-钢结构厂房-钢结构工程[江苏海逵钢构厂] | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 首页-浙江橙树网络技术有限公司| 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 专业的压球机生产线及解决方案厂家-河南腾达机械厂 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | 上海道勤塑化有限公司 | 双效节能浓缩器-热回流提取浓缩机组-温州市利宏机械 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, |