最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年01月30日 14:58:53   作者:天下沒有收費的bug  
這篇文章主要介紹了普通類注入不進spring bean的解決方法,幫助大家更好的理解和使用spring bean,感興趣的朋友可以了解下

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

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

1、引入這個springUtil類

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

貼上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
 */
@Component
public 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;
 }

 /**
  * 獲取對象
  *
  * @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。 如果與給定名字相應(yīng)的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();
 }
}

貼上調(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 和對應(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è)號
  if ("QYH".equals(type)) {

   //可以單獨傳入http參數(shù),這樣參數(shù)會自動做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ù)號
  if ("FWH".equals(type)) {

   //可以單獨傳入http參數(shù),這樣參數(shù)會自動做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參數(shù),這樣參數(shù)會自動做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)) {
   //可以單獨傳入http參數(shù),這樣參數(shù)會自動做URL編碼,拼接在URL中
   HashMap<String, Object> paramMap = new HashMap<>(16);
   paramMap.put("corpId", corpID);
   paramMap.put("corpSecret", secret);
   //請求微服務(wù)接口地址
   HttpRequest.delete(resUrl() + "/api/mobile/QYH")
     .form(paramMap).execute().body();

   return null;
  }

  if ("FWH".equals(type)) {
   //可以單獨傳入http參數(shù),這樣參數(shù)會自動做URL編碼,拼接在URL中
   HashMap<String, Object> paramMap = new HashMap<>(16);
   paramMap.put("appId", corpID);
   paramMap.put("appSecret", secret);
   //請求微服務(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);
   //請求微服務(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)) {
   //可以單獨傳入http參數(shù),這樣參數(shù)會自動做URL編碼,拼接在URL中
   HashMap<String, Object> paramMap = new HashMap<>(16);
   paramMap.put("corpId", corpID);
   paramMap.put("corpSecret", secret);
   //請求微服務(wù)接口地址
   String result = HttpUtil.get(resUrl() + "/api/mobile/QYH/isJSTicket", paramMap);

   return result;
  }

  if ("FWH".equals(type)) {
   //可以單獨傳入http參數(shù),這樣參數(shù)會自動做URL編碼,拼接在URL中
   HashMap<String, Object> paramMap = new HashMap<>(16);
   paramMap.put("appId", corpID);
   paramMap.put("appSecret", secret);
   //請求微服務(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);
   //請求微服務(wù)接口地址
   String result = HttpUtil.get(resUrl() + "/api/mobile/DING/isJSTicket", paramMap);

   return result;
  }
  return "";
 }
 /**
  * 獲取數(shù)據(jù)庫中的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ǔ)知識還要補,打卡現(xiàn)在是2020/12/16寫得博客,那天把這里得知識補了,在回來留痕。

以上就是普通類注入不進spring bean的解決方法的詳細(xì)內(nèi)容,更多關(guān)于普通類注入不進spring bean的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mybatis 創(chuàng)建方法、全局配置教程詳解

    Mybatis 創(chuàng)建方法、全局配置教程詳解

    MyBatis是一個半自動化的輕量級的持久化層框架。 MyBatis實際上是Ibatis3.0版本以后的持久化層框架,下面通過本文給大家分享Mybatis 創(chuàng)建方法、全局配置教程詳解,需要的朋友參考下吧
    2017-09-09
  • Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn)代碼

    Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn)代碼

    這篇文章主要介紹了Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • Go Java算法之解碼方法示例詳解

    Go Java算法之解碼方法示例詳解

    這篇文章主要為大家介紹了Go Java算法之解碼方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 如何判斷java是32位的還是64位的

    如何判斷java是32位的還是64位的

    這篇文章主要介紹了如何判斷java是32位的還是64位的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 在Java中Collection的一些常用方法總結(jié)

    在Java中Collection的一些常用方法總結(jié)

    今天給大家?guī)淼闹R是關(guān)于Java的,文章圍繞著Java中Collection的一些常用方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟

    Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟

    這篇文章主要介紹了Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 詳解JAVA Timer和TimerTask

    詳解JAVA Timer和TimerTask

    這篇文章主要介紹了JAVA Timer和TimerTask的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • IDEA連接MySQL數(shù)據(jù)庫的4種方法圖文教程

    IDEA連接MySQL數(shù)據(jù)庫的4種方法圖文教程

    IDEA是一種流行的Java開發(fā)工具,可以方便地連接MySQL,這篇文章主要給大家介紹了關(guān)于IDEA連接MySQL數(shù)據(jù)庫的4種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Java Mail郵件發(fā)送如何實現(xiàn)簡單封裝

    Java Mail郵件發(fā)送如何實現(xiàn)簡單封裝

    這篇文章主要介紹了Java Mail郵件發(fā)送如何實現(xiàn)簡單封裝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • @Value設(shè)置默認(rèn)值后,獲取不到配置值的原因分析

    @Value設(shè)置默認(rèn)值后,獲取不到配置值的原因分析

    這篇文章主要介紹了@Value設(shè)置默認(rèn)值后,獲取不到配置值的原因,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評論

邻水| 柳江县| 霍林郭勒市| 承德市| 花莲县| 昭觉县| 出国| 湖州市| 上蔡县| 泽州县| 栖霞市| 湖南省| 疏附县| 乐陵市| 定西市| 瑞金市| 从化市| 武夷山市| 深泽县| 广宗县| 凤冈县| 水城县| 周口市| 长寿区| 宁波市| 南通市| 延庆县| 敦煌市| 天水市| 波密县| 永嘉县| 瑞丽市| 四平市| 墨玉县| 剑川县| 余姚市| 炎陵县| 锦屏县| 论坛| 正阳县| 内江市|