nutz web项目启动时怎么加载IocEventListener接口(不是NB项目)
12 回复
@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);
}
}
@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);
}
}
没啊,换到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....");
没打印
添加回复
请先登陆