NutzCN Logo
问答 ioc没封装上???
发布于 2377天前 作者 蛋蛋的忧伤 1481 次浏览 复制 上一个帖子 下一个帖子
标签:

resources/ioc/conf.js

12 回复

稍等。。。。手快点错了....

竟然不支持编辑!!!还不支持删除......

resources/ioc/conf.js

var ioc = {
    conf : {
        type : "org.nutz.ioc.impl.PropertiesProxy",
        fields : {
            utf8  : false,
            paths : [ "config/" ]
        }
    }
}

然后我的一个类:
@IocBean
public class WxService {

@Inject
private PropertiesProxy conf;

....
}
这个类里拿conf时是null

但是我另一个类里就可以注入进去,好奇怪。。。

WxService实例是你自己new的吧

package com.yixin.gglm.service;

import com.sun.prism.impl.Disposer;
import org.nutz.dao.entity.Record;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;

import javax.servlet.http.HttpSession;

@IocBean
public class WxService {

    @Inject
    private PropertiesProxy conf;


    static Log log = Logs.get();


    public static void main(String[] args) throws Exception {
//        getOpenId("");
    }

    public String getOpenId(String code) throws Exception{
//        String realUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wxed8ad8b76654736c&secret=9358666a8fde6a5b91faa354e1d05e44&js_code=011OJVEN1Y3rJ515yGEN1hFQEN1OJVEg&grant_type=authorization_code";
//        HttpSession httpSession = Mvcs.getHttpSession();
//        Object openid = httpSession.getAttribute("openid");
//        if(openid != null){
//            return openid.toString();
//        }

        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        String realUrl = String.format(url, conf.get("wx.appid"), conf.get("wx.appsecret"), code);
        Response response = Http.get(realUrl);
        if(response.getStatus() == 200){
            log.debug(response.getContent());
            Record record = Json.fromJson(Record.class, response.getContent());
//            httpSession.setAttribute("openid",(String) record.get("openid"));
            return (String) record.get("openid");
        }else{
            return null;
        }
    }

    private static NutMap nm = new NutMap();
    public WxService(){
        init();
    }


    void init(){
        NutMap map = new NutMap();
        map.put("appid", conf.get("wx.appid"));
        map.put("secret", conf.get("wx.appsecret"));
        map.put("grant_type", "client_credential");
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s";
        url =  String.format(url,"client_credential",conf.get("wx.appid"),conf.get("wx.appsecret"));
        Response response = Http.get(url, 60000);
        if(response.isOK()){
            log.debug(response.getContent());
            NutMap nutMap = Json.fromJson(NutMap.class, response.getContent());
            nm.setv("time",System.currentTimeMillis());
            nm.setv("access_token",nutMap.get("access_token"));
        }
    }


    public String getAccess_token(){
        Object time = nm.get("time");
        if(time == null){
            return null;
        }
        synchronized (WxService.class){
            long l = System.currentTimeMillis() - (long)time;
            if(l > 7200000){//大于两个小时
                init();
                return String.valueOf(nm.get("access_token"));
            }else{
                return String.valueOf(nm.get("access_token"));
            }
        }
    }
}

我在构造函数里init方法里使用了conf,然后是null,我在getOpenId方法里conf有实例

怎么可能时我new的,那我也未免太菜了吧

这个WxService类的构造不走完,里面的属性都是null吗?

构造方法内不能使用需要注入的属性

@IocBean(create="init")

并去掉构造方法

好的大叔,是我文档不精.........

添加回复
请先登陆
回到顶部