av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

普通類注入不進(jìn)spring bean的解決方法

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

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

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

1、引入這個(gè)springUtil類

普通類注入不進(jìn)spring bean的解決方法

2、通過構(gòu)造方法注入

普通類注入不進(jìn)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管理環(huán)境中獲取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應(yīng)用上下文環(huán)境 */ private static ConfigurableListableBeanFactory beanFactory; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtils.beanFactory = beanFactory; } /** * 獲取對(duì)象 * * @param name * @return Object 一個(gè)以所給名字注冊(cè)的bean的實(shí)例 * @throws BeansException * */ @SuppressWarnings('unchecked') public static <T> T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } /** * 獲取類型為requiredType的對(duì)象 * * @param clz * @return * @throws BeansException * */ public static <T> T getBean(Class<T> clz) throws BeansException { T result = (T) beanFactory.getBean(clz); return result; } /** * 如果BeanFactory包含一個(gè)與所給名稱匹配的bean定義,則返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return beanFactory.containsBean(name); } /** * 判斷以給定名字注冊(cè)的bean定義是一個(gè)singleton還是一個(gè)prototype。 如果與給定名字相應(yīng)的bean定義沒有被找到,將會(huì)拋出一個(gè)異常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException * */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return beanFactory.isSingleton(name); } /** * @param name * @return Class 注冊(cè)對(duì)象的類型 * @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代理對(duì)象 * * @param invoker * @return */ @SuppressWarnings('unchecked') public static <T> T getAopProxy(T invoker) { return (T) AopContext.currentProxy(); }}

貼上調(diào)用得方法:

注意:調(diào)用getValidator()方法直接返回得是 AgentCfgDao agentCfgDao ,相當(dāng)于

@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 和對(duì)應(yīng)的 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); } /** * 根據(jù)corpID, secret 換取AccessToken * * @param corpID corpID * @param secret secret * @param type type * @return */ public String getAccessToken(String corpID, String secret, String type) { //如果是企業(yè)號(hào) if ('QYH'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做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; } //如果是服務(wù)號(hào) if ('FWH'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做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; } //如果是釘釘號(hào) if ('DING'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做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; } /** * 根據(jù)corpID, secret 刪除舊的token * * @param corpID * @param secret * @return */ public String delAccessToken(String corpID, String secret, String type) { if ('QYH'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('corpId', corpID); paramMap.put('corpSecret', secret); //請(qǐng)求微服務(wù)接口地址 HttpRequest.delete(resUrl() + '/api/mobile/QYH') .form(paramMap).execute().body(); return null; } if ('FWH'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('appId', corpID); paramMap.put('appSecret', secret); //請(qǐng)求微服務(wù)接口地址 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); //請(qǐng)求微服務(wù)接口地址 HttpRequest.delete(resUrl() + '/api/mobile/DING') .form(paramMap).execute().body(); return ''; } return ''; } /** * 根據(jù)corpID, secret 換取JSTicket * * @param corpID * @param secret * @return */ public String getJSTicket(String corpID, String secret, String type) { if ('QYH'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('corpId', corpID); paramMap.put('corpSecret', secret); //請(qǐng)求微服務(wù)接口地址 String result = HttpUtil.get(resUrl() + '/api/mobile/QYH/isJSTicket', paramMap); return result; } if ('FWH'.equals(type)) { //可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做URL編碼,拼接在URL中 HashMap<String, Object> paramMap = new HashMap<>(16); paramMap.put('appId', corpID); paramMap.put('appSecret', secret); //請(qǐng)求微服務(wù)接口地址 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); //請(qǐng)求微服務(wù)接口地址 String result = HttpUtil.get(resUrl() + '/api/mobile/DING/isJSTicket', paramMap); return result; } return ''; } /** * 獲取數(shù)據(jù)庫(kù)中的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; }}

總結(jié):bug是搞定了,但是基礎(chǔ)知識(shí)還要補(bǔ),打卡現(xiàn)在是2020/12/16寫得博客,那天把這里得知識(shí)補(bǔ)了,在回來留痕。

以上就是普通類注入不進(jìn)spring bean的解決方法的詳細(xì)內(nèi)容,更多關(guān)于普通類注入不進(jìn)spring bean的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: www.成人| 国产无遮挡又黄又爽免费网站 | 日韩精品久久久 | 国产尤物视频 | 色妞网 | 久久精品久久久精品美女 | 91超碰在线播放 | 日韩网站免费观看 | 国产日韩欧美在线观看 | 韩日一级片 | 国产区一区二区 | 看片地址| 二区三区视频 | 91午夜精品亚洲一区二区三区 | 成人在线免费看 | 女教师合集乱500篇小说 | 亚洲精品区 | www.夜夜| 激情五月激情综合网 | 午夜久久久久久 | av不卡一区| 久久久久久97 | 国产精品视频免费看 | 国产免费一区二区三区免费视频 | 久久天堂网 | 国产精品黄色 | 欧美激情自拍 | 午夜在线观看视频 | 欧美日韩在线一区 | 亚洲三区在线 | 中文字幕在线免费观看 | 91久久综合亚洲鲁鲁五月天 | 国产网站在线 | 亚洲h视频| 黄色片久久 | 日韩精品一区二区在线 | 美女在线播放 | 亚洲精品社区 | 不用播放器的av | 国产精品视频在线观看 | 一级片日韩 |