欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-08-31 10:44:57
解决问题:我在做移动端accesstoken的使用遇到一个问题,就是普通类死活注入不进去spring bean,我和同事雷杰通过各种注解,xml配置搞了好久都搞不定,这里插个眼,有空补一下spring...

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

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

1、引入这个springutil类

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

2、通过构造方法注入

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

贴上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的资料请关注其它相关文章!