NutzCN Logo
问答 关于Nutz注入问题
发布于 2945天前 作者 佐蓝 2597 次浏览 复制 上一个帖子 下一个帖子
标签:

@Iocbean
class A

Class B

@inject
A a

这里的a是可以注入的。

问题:
我发现当A继承类或者实现接口后,注入失败,如何处理?

19 回复

nutz版本是什么,贴报错信息

@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)

看上去是新开的线程, 然后new出来的对象, 所以没有注入一说

@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);
				}
			}
		});

应该如何处理呢?

GeneralPerfService systemPerfService = IocMaster.getIoc().get(GeneralPerfService.class);

@wendal
谢谢兽总,完美解决了

@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 {
						}
					}
				}

@qq_83d99869 反正不能new

@wendal
对不起手点快了,这个到 objModel2.validate(); 就出错了,请问是循环创建的AbstractModel导致的报错吗

出错,那出啥错呢?

@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)

那就是说subsetModelList里面的对象是new出来的咯

@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);
···
}

@qq_83d99869 记住一个规则,不是从ioc取出来的,就不会有注入相关的福利.

@wendal 谢谢兽总,问题解决了,记住了这个规则

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