NutzCN Logo
问答 关于Nutz环境下调用Spring下注入的service的问题
发布于 1075天前 作者 shufu 1375 次浏览 复制 上一个帖子 下一个帖子
标签:

我主要是在@IocBean 服务里面,需要调用Spring下定义的服务,解决用@Inject无效报错的问题
按照提示这样设置:

@AdaptBy(type = JsonAdaptor.class)
@IocBean(create = "init")
@Slf4j
@Modules(scanPackage = true)
@IocBy(args={
        "*js", "ioc/",
        "*anno", "com.podiy",
        "*spring"
})
public class MainLauncher {}

编译启动报错:

14:32:13.936 [com.podiy.app.auth.MainLauncher.main()] INFO org.nutz.ioc.impl.NutIoc - ... NutIoc init complete
14:32:13.967 [com.podiy.app.auth.MainLauncher.main()] ERROR org.nutz.boot.NbApp - something happen!!
java.lang.IllegalArgumentException: ServletContext must not be null
	at org.springframework.util.Assert.notNull(Assert.java:134)
	at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:108)
	at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:98)
	at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:81)
	at org.nutz.integration.spring.SpringIocLoader._init(SpringIocLoader.java:76)
	at org.nutz.integration.spring.SpringIocLoader.context(SpringIocLoader.java:64)
	at org.nutz.integration.spring.SpringIocLoader.has(SpringIocLoader.java:59)
	at org.nutz.ioc.loader.combo.ComboIocLoader.has(ComboIocLoader.java:144)
	at org.nutz.ioc.impl.NutIoc.has(NutIoc.java:275)
	at org.nutz.boot.NbApp.prepareIoc(NbApp.java:461)
	at org.nutz.boot.NbApp.prepare(NbApp.java:315)
	at org.nutz.boot.NbApp.execute(NbApp.java:201)
	at org.nutz.boot.NbApp.run(NbApp.java:182)
	at com.podiy.app.auth.MainLauncher.main(MainLauncher.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
	at java.lang.Thread.run(Thread.java:748)
14:32:13.967 [com.podiy.app.auth.MainLauncher.main()] INFO org.nutz.ioc.impl.NutIoc - org.nutz.ioc.impl.NutIoc@408456999 is closing. startup date [21-12-12 14:32:13.936]
14:32:13.967 [com.podiy.app.auth.MainLauncher.main()] INFO org.nutz.ioc.impl.NutIoc - org.nutz.ioc.impl.NutIoc@408456999 is deposed. startup date [21-12-12 14:32:13.936]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.814 s
[INFO] Finished at: 2021-12-12T14:32:13+08:00
[INFO] Final Memory: 35M/312M
[INFO] ------------------------------------------------------------------------
Disconnected from the target VM, address: '127.0.0.1:13689', transport: 'socket'
3 回复

这种操作很少见了吧

是呀,不得已呀,spring类里面也是嵌入引用,如果直接new的话,类里面的类都是null,而且嵌套了好几层,只有通过从spring中读出才能正常

把对象存到static变量里, 就不必走ioc了

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