NutzCN Logo
问答 nutz web项目启动时怎么加载IocEventListener接口
发布于 2418天前 作者 haoqoo 2050 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz web项目启动时怎么加载IocEventListener接口(不是NB项目)

12 回复

加@IocBean就好啦

ok,我试试

@IocBean
public class FeignIoc implements IocEventListener {

@Override
public Object afterBorn(Object obj, String s) {
    //log.info("------------web application afterBorn---------");
    try {

        Mirror mirror = Mirror.me(obj);
        for (Field field : obj.getClass().getDeclaredFields()) {
            FeignInject fc = field.getAnnotation(FeignInject.class);
            if (fc == null)
                continue;
            String url = "http://127.0.0.1:8090";
            Encoder encoder = getEncoder(fc, field);
            Decoder decoder = getDecoder(fc, field);

// Client client = getClient(fc, field, url);
Class apiType = field.getType();

            Feign.Builder builder = Feign.builder();
            if (encoder != null)
                builder.encoder(encoder);
            if (decoder != null)
                builder.decoder(decoder);

// if (client != null)
// builder.client(client);
// builder.logger(logger);
// builder.logLevel(level);
int connectTimeout = fc.connectTimeout();
if (connectTimeout == 0)
connectTimeout = 10 * 1000;
int readTimeout = fc.readTimeout();
if (readTimeout == 0)
readTimeout = 10 * 1000;
builder.options(new Request.Options(connectTimeout, readTimeout));
Object t = builder.target(apiType, url);

            mirror.setValue(obj, field.getName(), t);
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    return obj;
}

@Override
public Object afterCreate(Object o, String s) {
    return o;
}

@Override
public int getOrder() {
    return 0;
}


protected Decoder getDecoder(FeignInject fc, Field field) {
    String decoderStr = Strings.sBlank(fc.decoder(), "");
   /* switch (decoderStr) {
        case "":
        case "raw":
            break;
        case "nutzjson":
            return new NutzJsonDecoder();
        case "jackson":
            return new JacksonDecoder();
        case "gson":
            return new GsonDecoder();
        case "jaxb":
            JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8")
                    .withMarshallerSchemaLocation(getSchemaString(fc.schema()))
                    .build();
            return new JAXBDecoder(jaxbFactory);
        default:
            if (decoderStr.startsWith("ioc:"))
                return ioc.get(Decoder.class, decoderStr.substring(4));
            break;
    }*/
    return new NutzJsonDecoder();
}

protected Encoder getEncoder(FeignInject fc, Field field) {
    String encoderStr = Strings.sBlank(fc.encoder(), "");
/*    switch (encoderStr) {
        case "":
        case "raw":
            break;
        case "nutzjson":
            JsonFormat jf = JsonFormat.full();
            String jfStr = Strings.sBlank(fc.jsonFormat(), conf.get(PROP_JSON_FORMAT, ""));
            if (!Strings.isBlank(jfStr)) {
                if (jfStr.startsWith("{")) {
                    jf = Json.fromJson(JsonFormat.class, jfStr);
                }
                else if (jfStr.startsWith("ioc:")) {
                    jf = ioc.get(JsonFormat.class, jfStr.substring(4));
                }
                else {
                    try {
                        jf = (JsonFormat) JsonFormat.class.getMethod(jfStr).invoke(null);
                    }
                    catch (Exception e) {
                        log.infof("invaild JsonFormat=[%s] at %s", jfStr, field);
                    }
                }
            }
            return new NutzJsonEncoder(jf);
        case "jackson":
            return new JacksonEncoder();
        case "gson":
            return new GsonEncoder();
        case "jaxb":
            JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8")
                    .withMarshallerSchemaLocation(getSchemaString(fc.schema()))
                    .build();
            return new JAXBEncoder(jaxbFactory);
        default:
            if (encoderStr.startsWith("ioc"))
                return ioc.get(Encoder.class, encoderStr.substring(4));
            break;
    }*/
    JsonFormat jf = JsonFormat.full();

    return new NutzJsonEncoder(jf);
}

}

看到 "插入代码" 按钮没?

PS: 换最新的快照版

@IocBean
public class FeignIoc implements IocEventListener {

    @Override
    public Object afterBorn(Object obj, String s) {
        //log.info("------------web application afterBorn---------");
        try {

            Mirror mirror = Mirror.me(obj);
            for (Field field : obj.getClass().getDeclaredFields()) {
                FeignInject fc = field.getAnnotation(FeignInject.class);
                if (fc == null)
                    continue;
                String url = "http://127.0.0.1:8090";
                Encoder encoder = getEncoder(fc, field);
                Decoder decoder = getDecoder(fc, field);
//                Client client = getClient(fc, field, url);
                Class apiType = field.getType();

                Feign.Builder builder = Feign.builder();
                if (encoder != null)
                    builder.encoder(encoder);
                if (decoder != null)
                    builder.decoder(decoder);
//                if (client != null)
//                    builder.client(client);
//                builder.logger(logger);
//                builder.logLevel(level);
                int connectTimeout = fc.connectTimeout();
                if (connectTimeout == 0)
                    connectTimeout = 10 * 1000;
                int readTimeout = fc.readTimeout();
                if (readTimeout == 0)
                    readTimeout = 10 * 1000;
                builder.options(new Request.Options(connectTimeout, readTimeout));
                Object t = builder.target(apiType, url);

                mirror.setValue(obj, field.getName(), t);
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return obj;
    }

    @Override
    public Object afterCreate(Object o, String s) {
        return o;
    }

    @Override
    public int getOrder() {
        return 0;
    }


    protected Decoder getDecoder(FeignInject fc, Field field) {
        String decoderStr = Strings.sBlank(fc.decoder(), "");
       /* switch (decoderStr) {
            case "":
            case "raw":
                break;
            case "nutzjson":
                return new NutzJsonDecoder();
            case "jackson":
                return new JacksonDecoder();
            case "gson":
                return new GsonDecoder();
            case "jaxb":
                JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8")
                        .withMarshallerSchemaLocation(getSchemaString(fc.schema()))
                        .build();
                return new JAXBDecoder(jaxbFactory);
            default:
                if (decoderStr.startsWith("ioc:"))
                    return ioc.get(Decoder.class, decoderStr.substring(4));
                break;
        }*/
        return new NutzJsonDecoder();
    }

    protected Encoder getEncoder(FeignInject fc, Field field) {
        String encoderStr = Strings.sBlank(fc.encoder(), "");
    /*    switch (encoderStr) {
            case "":
            case "raw":
                break;
            case "nutzjson":
                JsonFormat jf = JsonFormat.full();
                String jfStr = Strings.sBlank(fc.jsonFormat(), conf.get(PROP_JSON_FORMAT, ""));
                if (!Strings.isBlank(jfStr)) {
                    if (jfStr.startsWith("{")) {
                        jf = Json.fromJson(JsonFormat.class, jfStr);
                    }
                    else if (jfStr.startsWith("ioc:")) {
                        jf = ioc.get(JsonFormat.class, jfStr.substring(4));
                    }
                    else {
                        try {
                            jf = (JsonFormat) JsonFormat.class.getMethod(jfStr).invoke(null);
                        }
                        catch (Exception e) {
                            log.infof("invaild JsonFormat=[%s] at %s", jfStr, field);
                        }
                    }
                }
                return new NutzJsonEncoder(jf);
            case "jackson":
                return new JacksonEncoder();
            case "gson":
                return new GsonEncoder();
            case "jaxb":
                JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8")
                        .withMarshallerSchemaLocation(getSchemaString(fc.schema()))
                        .build();
                return new JAXBEncoder(jaxbFactory);
            default:
                if (encoderStr.startsWith("ioc"))
                    return ioc.get(Encoder.class, encoderStr.substring(4));
                break;
        }*/
        JsonFormat jf = JsonFormat.full();

        return new NutzJsonEncoder(jf);
    }

}

被iocby扫描到没

没啊,换到66快照也没反应

@IocBean
public class FeignIoc implements IocEventListener {

    @Override
    public Object afterBorn(Object obj, String s) {
        //log.info("------------web application afterBorn---------");
        try {
            System.out.println("IocEventListener afterBorn....");

没打印

iocby需要扫描到它呢

iocby 是扫到了

@IocBy(type = ComboIocProvider.class,
        args = {
                "*js",
                "*anno",
                "com.yaoee",
                "*jedis",
                "*weixin",
                "*tx"
        })

FeignIoc 这个类在package com.yaoee.web包下。
但没执行afterborn吧

额,日志里面显示扫描到了?debug一下NutIoc的构造方法吧

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