欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

2、通過(guò)構(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工具類(lèi) 方便在非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;
 }

 /**
  * 獲取對(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);
 }

 /**
  * 獲取類(lèi)型為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è)與所給名稱(chēng)匹配的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定義沒(méi)有被找到,將會(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ì)象的類(lèi)型
  * @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寫(xiě)得博客,那天把這里得知識(shí)補(bǔ)了,在回來(lái)留痕。

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

相關(guān)文章

  • Java面向接口編程之命令模式實(shí)例詳解

    Java面向接口編程之命令模式實(shí)例詳解

    這篇文章主要介紹了Java面向接口編程之命令模式,結(jié)合實(shí)例形式詳細(xì)分析了Java面向接口編程命令模式的定義、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • 簡(jiǎn)單了解Java類(lèi)成員初始化順序

    簡(jiǎn)單了解Java類(lèi)成員初始化順序

    這篇文章主要介紹了簡(jiǎn)單了解Java類(lèi)成員初始化順序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java SpringMVC異步處理詳解

    Java SpringMVC異步處理詳解

    這篇文章主要介紹了Java springmvc的處理異步,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-10-10
  • Java模擬服務(wù)器解析web數(shù)據(jù)

    Java模擬服務(wù)器解析web數(shù)據(jù)

    本篇文章主要給大家詳細(xì)分享了搭建JavaWeb服務(wù)器的詳細(xì)步驟以及用到的代碼,對(duì)此有需要的朋友可以跟著學(xué)習(xí)下,希望能給你帶來(lái)幫助
    2021-07-07
  • SpringBoot集成Kafka的步驟

    SpringBoot集成Kafka的步驟

    這篇文章主要介紹了SpringBoot集成Kafka的步驟,幫助大家更好的理解和使用SpringBoot,感興趣的朋友可以了解下
    2021-01-01
  • Java 反射機(jī)制實(shí)例詳解

    Java 反射機(jī)制實(shí)例詳解

    這篇文章主要介紹了Java 反射機(jī)制實(shí)例詳解的相關(guān)資料,這里對(duì)java中反射機(jī)制進(jìn)行了詳細(xì)的分析,需要的朋友可以參考下
    2017-09-09
  • Spring的BeanFactoryPostProcessor接口示例代碼詳解

    Spring的BeanFactoryPostProcessor接口示例代碼詳解

    這篇文章主要介紹了Spring的BeanFactoryPostProcessor接口,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java虛擬機(jī)之類(lèi)加載

    Java虛擬機(jī)之類(lèi)加載

    這篇文章主要介紹了Java虛擬機(jī)之類(lèi)加載,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • java加載屬性配置properties文件的方法

    java加載屬性配置properties文件的方法

    這篇文章主要介紹了java加載屬性配置properties文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Spring注解驅(qū)動(dòng)開(kāi)發(fā)實(shí)現(xiàn)屬性賦值

    Spring注解驅(qū)動(dòng)開(kāi)發(fā)實(shí)現(xiàn)屬性賦值

    這篇文章主要介紹了Spring注解驅(qū)動(dòng)開(kāi)發(fā)實(shí)現(xiàn)屬性賦值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論