zoukankan      html  css  js  c++  java
  • 使用@Component注解时 @Autowired注入为null怎么解决

    问题代码:

    /**
     * 天地图工具类
     *
     * @author ywy
     * @date 2020-08-12
     */
    @Component
    public class TmapUtil {
    
        @Autowired
        private TmapConfiguration tmapConfiguration;
    
        /**
         * 根据地名获取经纬度
         *
         * @param addr 查询关键字
         * @author ywy
         * @date 2020-08-12
         * @return Map<String, Object>
         * @throws Exception
         */
        public static Map<String, Object> getCoordinateByAddr(String addr) throws Exception {
            Map<String, Object> coordinate = new HashMap<>();
            tmapConfiguration.XXMethod();// 这里会报错tmapConfiguration为null
            /*省略代码*/
            return coordinate;
        }
    } 
    

     

    解决代码:

    /** 
     * 天地图工具类
     *
     * @author ywy
     * @date 2020-08-12
     */
    @Component
    public class TmapUtil {
    
        @Autowired
        private TmapConfiguration tmapConfiguration;
    
      /*添加代码 begin*/
        private static TmapUtil tmapUtil; 
    
        @PostConstruct
        public void init(){
            tmapUtil = this;
            tmapUtil.tmapConfiguration = this.tmapConfiguration;
        }
      /*添加代码 end*/
    
        /**
         * 根据地名获取经纬度
         *
         * @param addr 查询关键字
         * @author ywy
         * @date 2020-08-12
         * @return Map<String, Object>
         * @throws Exception
         */
        public static Map<String, Object> getCoordinateByAddr(String addr) throws Exception {
            Map<String, Object> coordinate = new HashMap<>();
         // 更改调用方式
            tmapUtil.tmapConfiguration.XXMethod();// 这里tmapConfiguration作为tmapUtil的属性调用 不再是null了
            /*省略代码*/
            return coordinate;
        }
    }   
    

     

  • 相关阅读:
    linq
    存储过程动态显示top条数
    js正则表达式
    WebClient异步下载文件
    C++ socket编程基础(ZT)
    Oracle数据导入导出imp/exp命令 10g以上expdp/impdp命令(转载)
    C#获取各种格式的系统时间
    C++中对sprintf()函数的说明(转)
    史上最全前端面试题(含答案)B篇
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/ywy8/p/13500624.html
Copyright © 2011-2022 走看看