@Iocbean
class A
Class B
@inject
A a
这里的a是可以注入的。
问题:
我发现当A继承类或者实现接口后,注入失败,如何处理?
@wendal
版本是1.r.58
报空指针异常,没有注入
11-29 14:54:05.002 WARN [SystemConfig] com.csg.itms.edic.util.core.EDIConsumer [handleMsg]
- There is an exception caused by java.lang.NullPointerException
at com.nari.nwims.busdown.busin.config.abstractmodel.AbstractModel.validate(Unknown Source)
at com.nari.nwims.busdown.busin.config.control.NWIMSApp.testJsonToBean(NWIMSApp.java:288)
at com.nari.nwims.busdown.busin.config.service.GeneralConfService.saveConf(GeneralConfService.java:50)
at com.nari.nwims.busdown.busin.system.receiver.SystemConfMessageReceiver.onMessageReceived(SystemConfMessageReceiver.java:53)
at com.csg.itms.edic.util.core.EDIConsumer.handleMsg(EDIConsumer.java:195)
at com.csg.itms.edic.util.core.EDIConsumer.access$2(EDIConsumer.java:158)
at com.csg.itms.edic.util.core.EDIConsumer$ScanQueueThread.run(EDIConsumer.java:140)
at java.lang.Thread.run(Thread.java:745)
@wendal 是的,我们都是这种new出来的,如何处理呢
@wendal 是的,我们都是这种new出来的,如何处理呢
@wendal
我们在非MVC下搭建的框架,创建如下
private static Ioc ioc = null;
private IocMaster() {
throw new RuntimeException("Shit! What are you doing?!");
}
public static Ioc getInstance() {
if (null != ioc) {
return ioc;
}
try {
ioc = new NutIoc(new ComboIocLoader("*com.nari.nwims.busdown.base.nutz.ExtConfLoader", "*js", ".", "*anno",
"com.nari.nwims.busdown", "*tx"));
} catch (ClassNotFoundException e) {
String msg = "create ioc failed...";
LOGGER.error(msg);
LOGGER.error(e.getMessage());
throw new RuntimeException(e);
}
return ioc;
}
由于业务的需要,是多线程处理,代码如下:
handleThreadPool.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName(threadName);
ThreadContext.put("threadName", Thread.currentThread().getName());
try {
long start = System.currentTimeMillis();
final Map<String, String> msg = getBusContent(request);
GeneralPerfService systemPerfService = new GeneralPerfService();
systemPerfService.saveSysPerfData(msg);
long end = System.currentTimeMillis();
log.info("系统性能单个队列花费的时间为 : " + (end - start) + " ms");
} catch (Exception e) {
log.error("系统性能入库发生异常", e);
}
}
});
应该如何处理呢?
@wendal
兽总,这里有个循环,在AbstractModel注入
@Inject
private ResourceEnumCache resourceEnumCache;
resourceEnumCache是空的,
和这个循环有关系吗。
for (AbstractModel objModel2 : subsetModelList) {
if (objModel2 != null) {
logger.debug("开始校验实体 ..... ");
objModel2.validate();
if (!objModel2.hasErrors()) {
logger.debug("检验通过 ....");
objModel2.save(new SubsetJdbcImpl(), new DbFuncOracleImpl());
} else {
}
}
}
@wendal
validate()里面有个
boolean contains = resourceEnumCache.contains(tableName, fieldName, value);
@Inject
private ResourceEnumCache resourceEnumCache;
resourceEnumCache还是空的,又报空指针
11-29 16:02:55.369 WARN [11111] com.csg.itms.edic.util.core.EDIConsumer [handleMsg]
- There is an exception caused by java.lang.NullPointerException
at com.nari.nwims.busdown.busin.config.abstractmodel.AbstractModel.validate(AbstractModel.java:117)
at com.nari.nwims.busdown.busin.config.control.NWIMSApp.testJsonToBean(NWIMSApp.java:244)
at com.nari.nwims.busdown.busin.config.service.GeneralConfService.saveConf(GeneralConfService.java:56)
at com.nari.nwims.busdown.busin.system.receiver.SystemConfMessageReceiver.onMessageReceived(SystemConfMessageReceiver.java:53)
at com.csg.itms.edic.util.core.EDIConsumer.handleMsg(EDIConsumer.java:195)
at com.csg.itms.edic.util.core.EDIConsumer.access$2(EDIConsumer.java:158)
at com.csg.itms.edic.util.core.EDIConsumer$ScanQueueThread.run(EDIConsumer.java:140)
at java.lang.Thread.run(Thread.java:745)
@wendal 不懂这样算不算new出来的
private ObjectMapper mapper = new ObjectMapper();
public void testJsonToBean(String jsonText) {
···
List<AbstractModel> subsetModelList = new ArrayList<AbstractModel>();
AbstractModel objModel = null;
···
objModel = (AbstractModel) mapper.convertValue(objJson,
Class.forName(AbstractModel.MODEL_PACKAGE + key));
subsetModelList.add(objModel);
···
}
@wendal 谢谢兽总,问题解决了,记住了这个规则