普通类注入spring bean的问题
解决问题:我在做移动端accessToken的使用遇到一个问题,就是普通类死活注入不进去spring bean,我和同事雷杰通过各种注解,xml配置搞了好久都搞不定,这里插个眼,有空补一下spring,得深入研究一下
解决办法:后面通过一个spring工具类搞定,这里贴上代码
1、引入这个springUtil类
2、通过构造方法注入
贴上SpringUtils代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.dt.base.weixin.util; 2 3 import org.springframework.aop.framework.AopContext; 4 import org.springframework.beans.BeansException; 5 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 6 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 7 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 8 import org.springframework.stereotype.Component; 9 10 /** 11 * @Description: spring工具类 方便在非spring管理环境中获取bean 12 * @author: ZhangChongHu 13 * @Date: 2020/12/8 17:23 14 * @Copyright: Xi'an Dian Tong Software Co., Ltd. All Rights Reserved. 15 * @Version 1.0 16 */ 17 @Component 18 public final class SpringUtils implements BeanFactoryPostProcessor 19 { 20 /** Spring应用上下文环境 */ 21 private static ConfigurableListableBeanFactory beanFactory; 22 23 @Override 24 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException 25 { 26 SpringUtils.beanFactory = beanFactory; 27 } 28 29 /** 30 * 获取对象 31 * 32 * @param name 33 * @return Object 一个以所给名字注册的bean的实例 34 * @throws BeansException 35 * 36 */ 37 @SuppressWarnings("unchecked") 38 public static <T> T getBean(String name) throws BeansException 39 { 40 return (T) beanFactory.getBean(name); 41 } 42 43 /** 44 * 获取类型为requiredType的对象 45 * 46 * @param clz 47 * @return 48 * @throws BeansException 49 * 50 */ 51 public static <T> T getBean(Class<T> clz) throws BeansException 52 { 53 T result = (T) beanFactory.getBean(clz); 54 return result; 55 } 56 57 /** 58 * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true 59 * 60 * @param name 61 * @return boolean 62 */ 63 public static boolean containsBean(String name) 64 { 65 return beanFactory.containsBean(name); 66 } 67 68 /** 69 * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) 70 * 71 * @param name 72 * @return boolean 73 * @throws NoSuchBeanDefinitionException 74 * 75 */ 76 public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException 77 { 78 return beanFactory.isSingleton(name); 79 } 80 81 /** 82 * @param name 83 * @return Class 注册对象的类型 84 * @throws NoSuchBeanDefinitionException 85 * 86 */ 87 public static Class<?> getType(String name) throws NoSuchBeanDefinitionException 88 { 89 return beanFactory.getType(name); 90 } 91 92 /** 93 * 如果给定的bean名字在bean定义中有别名,则返回这些别名 94 * 95 * @param name 96 * @return 97 * @throws NoSuchBeanDefinitionException 98 * 99 */ 100 public static String[] getAliases(String name) throws NoSuchBeanDefinitionException 101 { 102 return beanFactory.getAliases(name); 103 } 104 105 /** 106 * 获取aop代理对象 107 * 108 * @param invoker 109 * @return 110 */ 111 @SuppressWarnings("unchecked") 112 public static <T> T getAopProxy(T invoker) 113 { 114 return (T) AopContext.currentProxy(); 115 } 116 }
贴上调用得方法:
注意:调用getValidator()方法直接返回得是 AgentCfgDao agentCfgDao ,相当于
1 @Autowired 2 private AgentCfgDao agentCfgDao;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Copyright (c) 2014 - 2016 Xi'an Dian Tong Software Co., Ltd. All Rights Reserved. 3 * <p> 4 * This software is the confidential and proprietary information of Xi'an Dian Tong 5 * Software Co., Ltd. ("Confidential Information"). You shall not disclose such 6 * Confidential Information and shall use it only in accordance with the terms 7 * of the license agreement you entered into with Xi'an Dian Tong Software Co., Ltd. 8 */ 9 10 package com.dt.base.weixin.app; 11 12 import cn.hutool.http.HttpRequest; 13 import cn.hutool.http.HttpUtil; 14 import com.dt.base.weixin.util.SpringUtils; 15 import com.dt.ncfg.dao.AgentCfgDao; 16 import com.dt.sys.manage.entity.DtwxAgentCfg; 17 import org.apache.logging.log4j.LogManager; 18 import org.apache.logging.log4j.Logger; 19 import org.springframework.stereotype.Component; 20 import java.util.HashMap; 21 22 /** 23 * 保存了 corpID + secret 和对应的 access token 。 24 * key: corpID + secret 25 * value: access token 26 */ 27 28 public class AccessTokenPool { 29 30 protected final static Logger log = LogManager.getLogger("AccessTokenPool"); 31 32 DtwxAgentCfg dtwxAgentCfg = null; 33 34 35 /** 36 * 获取AgentCfgDao 37 * 38 * @return 39 */ 40 protected AgentCfgDao getValidator() { 41 return SpringUtils.getBean(AgentCfgDao.class); 42 } 43 44 /** 45 * 根据corpID, secret 换取AccessToken 46 * 47 * @param corpID corpID 48 * @param secret secret 49 * @param type type 50 * @return 51 */ 52 public String getAccessToken(String corpID, String secret, String type) { 53 54 //如果是企业号 55 if ("QYH".equals(type)) { 56 57 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 58 HashMap<String, Object> paramMap = new HashMap<>(); 59 paramMap.put("corpId", corpID); 60 paramMap.put("corpSecret", secret); 61 String result = HttpUtil.get(resUrl() + "/api/mobile/QYH/isExist", paramMap); 62 return result; 63 } 64 //如果是服务号 65 if ("FWH".equals(type)) { 66 67 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 68 HashMap<String, Object> paramMap = new HashMap<>(); 69 paramMap.put("appId", corpID); 70 paramMap.put("appSecret", secret); 71 72 String result = HttpUtil.get(resUrl() + "/api/mobile/FWH/isExist", paramMap); 73 return result; 74 } 75 //如果是钉钉号 76 if ("DING".equals(type)) { 77 78 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 79 HashMap<String, Object> paramMap = new HashMap<>(); 80 paramMap.put("appKey", corpID); 81 paramMap.put("appSecret", secret); 82 83 String result = HttpUtil.get(resUrl() + "/api/mobile/DING/isExist", paramMap); 84 return result; 85 } 86 return null; 87 } 88 89 90 91 /** 92 * 根据corpID, secret 删除旧的token 93 * 94 * @param corpID 95 * @param secret 96 * @return 97 */ 98 public String delAccessToken(String corpID, String secret, String type) { 99 100 if ("QYH".equals(type)) { 101 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 102 HashMap<String, Object> paramMap = new HashMap<>(16); 103 paramMap.put("corpId", corpID); 104 paramMap.put("corpSecret", secret); 105 //请求微服务接口地址 106 HttpRequest.delete(resUrl() + "/api/mobile/QYH") 107 .form(paramMap).execute().body(); 108 109 return null; 110 } 111 112 if ("FWH".equals(type)) { 113 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 114 HashMap<String, Object> paramMap = new HashMap<>(16); 115 paramMap.put("appId", corpID); 116 paramMap.put("appSecret", secret); 117 //请求微服务接口地址 118 HttpRequest.delete(resUrl() + "/api/mobile/FWH") 119 .form(paramMap).execute().body(); 120 return null; 121 } 122 123 if ("DING".equals(type)) { 124 HashMap<String, Object> paramMap = new HashMap<>(16); 125 paramMap.put("appKey", corpID); 126 paramMap.put("appSecret", secret); 127 //请求微服务接口地址 128 HttpRequest.delete(resUrl() + "/api/mobile/DING") 129 .form(paramMap).execute().body(); 130 return ""; 131 } 132 return ""; 133 } 134 135 /** 136 * 根据corpID, secret 换取JSTicket 137 * 138 * @param corpID 139 * @param secret 140 * @return 141 */ 142 public String getJSTicket(String corpID, String secret, String type) { 143 144 if ("QYH".equals(type)) { 145 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 146 HashMap<String, Object> paramMap = new HashMap<>(16); 147 paramMap.put("corpId", corpID); 148 paramMap.put("corpSecret", secret); 149 //请求微服务接口地址 150 String result = HttpUtil.get(resUrl() + "/api/mobile/QYH/isJSTicket", paramMap); 151 152 return result; 153 } 154 155 if ("FWH".equals(type)) { 156 //可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中 157 HashMap<String, Object> paramMap = new HashMap<>(16); 158 paramMap.put("appId", corpID); 159 paramMap.put("appSecret", secret); 160 //请求微服务接口地址 161 String result = HttpUtil.get(resUrl() + "/api/mobile/FWH/isJSTicket", paramMap); 162 163 return result; 164 } 165 166 if ("DING".equals(type)) { 167 HashMap<String, Object> paramMap = new HashMap<>(16); 168 paramMap.put("appKey", corpID); 169 paramMap.put("appSecret", secret); 170 //请求微服务接口地址 171 String result = HttpUtil.get(resUrl() + "/api/mobile/DING/isJSTicket", paramMap); 172 173 return result; 174 } 175 return ""; 176 } 177 /** 178 * 获取数据库中的url 179 * @return url 地址 180 */ 181 public String resUrl(){ 182 //获取url 183 DtwxAgentCfg dtwxAgentCfg = new DtwxAgentCfg(); 184 dtwxAgentCfg.setAppType("wxInterfaceUrl"); 185 dtwxAgentCfg.setConfigKey("RESQUEST_ACS_TOKEN"); 186 DtwxAgentCfg agentCfg = getValidator().selectDataCfg(dtwxAgentCfg); 187 //url=http://localhost:8080 188 String url = agentCfg.getConfigValue(); 189 return url; 190 } 191 192 193 }
总结:bug是搞定了,但是基础知识还要补,打卡现在是2020/12/16写得博客,那天把这里得知识补了,在回来留痕。
作者:天下没有收费的bug
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。