普通类注入不进spring bean的解决方法

 更新时间:2021年1月31日 15:00  点击:2020

解决问题:我在做移动端accessToken的使用遇到一个问题,就是普通类死活注入不进去spring bean,我和同事雷杰通过各种注解,xml配置搞了好久都搞不定,这里插个眼,有空补一下spring,得深入研究一下

解决办法:后面通过一个spring工具类搞定,这里贴上代码

1、引入这个springUtil类

2、通过构造方法注入

贴上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
 */
@Component
public 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的资料请关注猪先飞其它相关文章!

[!--infotagslink--]

相关文章

  • phpems SQL注入(cookies)分析研究

    PHPEMS(PHP Exam Management System)在线模拟考试系统基于PHP+Mysql开发,主要用于搭建模拟考试平台,支持多种题型和展现方式,是国内首款支持题冒题和自动评分与教师评分相...2016-11-25
  • ASP/PHP sql注入语句整理大全

    SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作 标准注入语句1.判...2016-11-25
  • Spring AOP 对象内部方法间的嵌套调用方式

    这篇文章主要介绍了Spring AOP 对象内部方法间的嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-29
  • PHP防止SQL注入的例子

    防止SQL注入是我们程序开发人员必须要做的事情了,今天我们就来看一篇关于PHP防止SQL注入的例子了,具体的实现防过滤语句可以参考下面来看看吧。 使用prepared以及参...2016-11-25
  • Spring Cloud 中@FeignClient注解中的contextId属性详解

    这篇文章主要介绍了Spring Cloud 中@FeignClient注解中的contextId属性详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-25
  • Springboot如何实现Web系统License授权认证

    这篇文章主要介绍了Springboot如何实现Web系统License授权认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-28
  • 如何在Spring WebFlux的任何地方获取Request对象

    这篇文章主要介绍了如何在Spring WebFlux的任何地方获取Request对象,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下...2021-01-26
  • 详解SpringCloudGateway内存泄漏问题

    这篇文章主要介绍了详解SpringCloudGateway内存泄漏问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-16
  • Spring为什么不推荐使用@Autowired注解详析

    @Autowired 注解的主要功能就是完成自动注入,使用也非常简单,但这篇文章主要给大家介绍了关于Spring为什么不推荐使用@Autowired注解的相关资料,需要的朋友可以参考下...2021-11-03
  • Springboot如何使用mybatis实现拦截SQL分页

    这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19
  • SpringMVC文件上传原理及实现过程解析

    这篇文章主要介绍了SpringMVC文件上传原理及实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-15
  • PHP中自带函数过滤sql注入代码分析

    SQL注入攻击是黑客攻击网站最常用的手段。如果你的站点没有使用严格的用户输入检验,那么常容易遭到SQL注入攻击。SQL注入攻击通常通过给站点数据库提交不良的数据或...2016-11-25
  • Spring Data JPA 关键字Exists的用法说明

    这篇文章主要介绍了Spring Data JPA 关键字Exists的用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-10
  • AngularJS 依赖注入详解及示例代码

    本文主要介绍AngularJS 依赖注入的知识,这里整理了相关的基础知识,并附示例代码和实现效果图,有兴趣的小伙伴可以参考下...2016-08-24
  • tomcat启动完成执行 某个方法 定时任务(Spring)操作

    这篇文章主要介绍了tomcat启动完成执行 某个方法 定时任务(Spring)操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25
  • Springboot实现多线程注入bean的工具类操作

    这篇文章主要介绍了Springboot实现多线程注入bean的工具类操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-27
  • 使用Maven 搭建 Spring MVC 本地部署Tomcat的详细教程

    这篇文章主要介绍了使用Maven 搭建 Spring MVC 本地部署Tomcat,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-08-16
  • Java Spring Cloud 负载均衡详解

    这篇文章主要介绍了Spring Cloud负载均衡及远程调用实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2021-09-18
  • SpringMvc自动装箱及GET请求参数原理解析

    这篇文章主要介绍了SpringMvc自动装箱及GET请求参数原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-19
  • Springboot使用thymeleaf动态模板实现刷新

    这篇文章主要介绍了Springboot使用thymeleaf动态模板实现刷新,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-08-31