Bootstrap

static与 Autowire 的问题

autowire注入的是实例对象,如果直接在static String getJsonStrFromDb( HashMap hashMap)中使用,则会报空指针。
那么如何解决呢?
想办法将实例对象注入到类对象就行了。解决思路,定义一个静态的属性,方便在static方法中使用,定义一个非static属性,在 init方法中进行替换。

public void init(){
rcsAccountService = rcsAccountService2;
rcsGroupService = rcsGroupService2;
}

@Slf4j
@Component
public class RcsParaInfoFromDbUtil {

    private static RcsAccountService rcsAccountService;
    private static RcsGroupService rcsGroupService;
    @Autowired
    private  RcsAccountService rcsAccountService2;    
    @Autowired
    private  RcsGroupService rcsGroupService2;
    @PostConstruct
    public void init(){
        rcsAccountService = rcsAccountService2;
        rcsGroupService = rcsGroupService2;
    }
    /**

     * @param hashMap
     * @return
     */
    public static String getJsonStrFromDb( HashMap hashMap) {
        String str = "";
        Integer type = (Integer) hashMap.get("type");
        if (type == 1) {
            List<RcsAccount> rcsAccountList = rcsAccountService.getJsonStrInfo(hashMap);
            log.info("从数据库RCS_ACCOUNT查询返回的结果:", rcsAccountList);
            if (null != rcsAccountList) {
                str = new Gson().toJson(rcsAccountList);
                return str;
            }
        }
        if (type == 3) {
            List<RcsGroup> rcsGroupList = rcsGroupService.getJsonStrInfo(hashMap);
            log.info("从数据库RCS_GROUP查询返回的结果:", rcsGroupList);
            if (null != rcsGroupList) {
                str = new Gson().toJson(rcsGroupList);
                return str;
            }
        }
        return str;
    }
}
;