普通类注入不进spring bean的解决方法
作者:天下没有收费的bug 发布时间:2021-06-07 19:22:41
标签:spring,bean,注入,普通类
解决问题:我在做移动端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写得博客,那天把这里得知识补了,在回来留痕。
来源:https://www.cnblogs.com/LoveBB/articles/14144056.html
0
投稿
猜你喜欢
- 在app中图片的轮播显示可以说是非常常见的实现效果了,其实现原理不过是利用ViewPager,然后利用handler每隔一定的时间将View
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 麦洛开通博客以来,有一段时间没有更新博文了.主要是麦洛这段时间因项目开发实在太忙了.今天周六还在公司加班,苦逼程序猿都是这样生活的.今天在做
- 这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据
- 1、使用场景 因为最近项目需要国际化,需要能够支持多种国际化语言,目前需要支持三种(法
- 多表联合查询resultType的返回值一般数据按参数类型返回<select id="queryCarIdList"
- 不知道大家对千篇一律的404 Not Found的错误页面是否感到腻歪了?其实通过很简单的配置就能够让Spring MVC显示您自定义的40
- 本文实例讲述了Android弹出窗口实现方法。分享给大家供大家参考,具体如下:直接上代码:/*** 弹窗--新手指引* @param cxt
- 什么是JDBCJDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java AP
- Activity设置全屏和无标题栏,要用到andorid.view.Window和Android.view.WindowManager。 W
- 循环例子:while循环和do…while循环whlie(条件语句) {循环体}//先进行条件语句的判断,再进行循环体do {循环体}whl
- step1. 导包(导入要使用的标签的jar文件)。step2. 使用taglib指令引入要使用的标签。taglib指令: &nb
- Java中的我们可以利用split把字符串按照指定的分割符进行分割,然后返回字符串数组split 方法该方法的作用是:将一个字符串分割为子字
- 前言好记性不如烂笔头,日期时间类那么花哨不如记下来多看两眼。提示:以下是本篇文章正文内容,下面案例可供参考一.日期时间类的包代码如下(示例)
- 代码如下:using System; using System.Collections.Generic; using S
- 闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能。于是乎开始学习.Net Remoting..Net Remoting 是由客户端通过R
- 将一个字符串转化成String[]数组,提供两种方法前言将字符串转化成数组提供两种方法:1.split("");2.to
- 本文实例讲述了Android开发实现SubMenu选项菜单和子菜单。分享给大家供大家参考,具体如下:简介:SubMenu:代表一个子菜单,包
- 最近需要做一个类似于电话客户的功能,要求拨打电话能自动录音。所以写了一个dome,希望能够帮到大家。主要思路就是监听手机通话状态在监听到接听
- 1.sonarQube的简介SonarQube是一款自动化代码审查工具,用于检测代码中的错误、漏洞和代码异味。它可以与你现有的工作流集成,以