NutzCN Logo
问答 nutz bean 加载失败的问题
发布于 2618天前 作者 qq_5abd8bce 2497 次浏览 复制 上一个帖子 下一个帖子
标签:

在mvcSetUp 启动加载 IOC bean,偶尔会出现创建bean失败的问题,但极大多数启动,都没问题的,不知道,这与加载的顺序有关吗??
以下是我使用的过程:

@IocBy(type = ComboIocProvider.class, args = {"*org.nutz.ioc.loader.json.JsonLoader", "ioc",
"*org.nutz.ioc.loader.annotation.AnnotationIocLoader", "x.x.x.x"})
@Encoding(input = "utf8", output = "utf8")
@Modules(scanPackage = true)
@SetupBy(MvcSetup.class)
@ChainBy(args = "ioc/chain.js")
@Chain("mainChain")
@Localization("msg")
@Views(BaseViewMaker.class)
@Ok("json")
@Fail("json")
public class MainModule {
}

public class MvcSetup implements Setup {
public void init(NutConfig config) {
....
CommonContext.ioc = config.getIoc();
X.register(CommonContext.ioc.get(Y.class));
X.register(CommonContext.ioc.get(Z.class));
.....
}
}

package x.x.x.x.x
@IocBean
public class Y {
private K k= CommonContext.ioc.get(K .class);
....
}

package x.x.x.x.x
@IocBean
public class Z {
private H h= CommonContext.ioc.get(H .class);
...
}

报错信息:
2017-09-14 23:02:28,728 [localhost-startStop-1] ERROR x.x.x.x.x.MvcSetup.init(MvcSetup.java:72) MvcSetup|init|fail|"errorMessage"="[Z] # FAIL to create Ioc Bean name=[Z]"
org.nutz.ioc.IocException: [Z] # FAIL to create Ioc Bean name=[Z]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:150)
at x.x.x.x.x.MvcSetup.init(MvcSetup.java:64)
at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:276)
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:120)
......
Caused by: org.nutz.lang.born.BorningException: Fail to born 'x.x.x.x.x.Z' becasue: java.lang.NullPointerException
at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:19)
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
... 26 more

疑惑的问题一:Y 和 Z 类基本都差不多的加载,为啥Y加载没问题,而Z加载偶尔报错。

29 回复

什么版本??

private K k= CommonContext.ioc.get(K .class);

这是什么操作。。。。 用@Inject啊

CommonContext.ioc.get(K .class)就是获取bean的对象操作。
用@Inject会报错,因为这个是启动类,IOC尚未加载bean

不管怎么依赖,这样声明是不行的

不会呀,nutz的使用文档就有这种注入的方式说明

怎么监听
通过对象自身的一个函数

这个函数必须为 public,并且不能有参数
为 UserService 对象增加一个函数:

public void init() {
    init_sysuser();
    init_cmsuser();
    init_roles();
    log.debug("sysuer/cmsuser/roles init complete");
}

在 UserService 配置文件中增加新的对象

@IocBean(create="init")
public class UserService {

...
}

调用代码,或第一次被取出时

ioc.get(UserService.class);

控制台输出

2016-12-15 DEBUG UserService(UserService.java:1024) : sysuer/cmsuser/roles init complete

ioc.get(UserService.class);

我就是用这种方式获取对象的bean的

这种写法是不支持的, 它能工作只是意外.

@IocBean
public classs ABC {
    private K k= CommonContext.ioc.get(K .class);
}

特指标注@IocBean的类的属性, 通过ioc.get获取另外一个bean作为它的值

就是这样子使用呢。那个类有@IocBean注解的

问题就是:那个类的加载,大多数启动,是没报错的,但是偶尔启动的时候,会报这个错误

2017-09-14 23:02:28,728 [localhost-startStop-1] ERROR x.x.x.x.x.MvcSetup.init(MvcSetup.java:72) MvcSetup|init|fail|"errorMessage"="[Z] # FAIL to create Ioc Bean name=[Z]"
org.nutz.ioc.IocException: [Z] # FAIL to create Ioc Bean name=[Z]
....
Caused by: org.nutz.lang.born.BorningException: Fail to born 'x.x.x.x.x.Z' becasue: java.lang.NullPointerException
at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:19)

每次抱着错,我都得重启来解决问题,但是,我现在想知道为啥会出现这个错误呢,空参数构造方法,因为是默认这种构造方法,所以那个类是没加构造方法的。

而且有:Y 和 Z 类,Y的这种方法获取都没报过错,就唯独 Z 偶尔报错呢

这样写是不对的. @Inject才是正路.

用@Inject这个,必须得注入之后,才可以,要不会报空指针。
MvcSetup这个是启动的类,初始化IOC,注入bean,是init里面执行的,倘若前面用@Inject这个来注入bean,会由于bean未加载,而导致注入为空,后面bean对象,会报空呢。
例如以下代码:StartTaskHandler 这个类用 @Inject注解注入,init方法,调用这个类的getKey()

public class MvcSetup implements Setup {

    private Log log = Logs.get();

    @Inject
    private StartTaskHandler startTaskHandler;

    public void init(NutConfig config) {
        try {
            ....
			
            CommonContext.ioc = config.getIoc();
            
            startTaskHandler.getKey();
			
			...
           
        } catch (Exception e) {
            log.error(new LogMessage(this.getClass().getSimpleName(), "init").fail(e.getMessage()), e);
        }
    }
2017-09-19 11:33:15,832 [main] ERROR x.x.x.x.api.MvcSetup.init(MvcSetup.java:76) MvcSetup|init|fail|"errorMessage"="null"
java.lang.NullPointerException
	at com.asiainfo.womail.nc.api.MvcSetup.init(MvcSetup.java:55)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:276)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:120)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at org.eclipse.jetty.servlet.FilterHolder.initialize(FilterHolder.java:137)
	at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:831)
	at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:300)
	at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1341)
	at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1334)
	at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:744)
	at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:497)
	at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:281)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:154)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
	at org.eclipse.jetty.server.Server.start(Server.java:357)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
	at org.eclipse.jetty.server.Server.doStart(Server.java:324)
	at org.eclipse.jetty.maven.plugin.JettyServer.doStart(JettyServer.java:68)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:564)
	at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:360)
	at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:168)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
	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.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Ioc ioc = config.getIoc();
StartTaskHandler startTaskHandler = ioc.get(StartTaskHandler.class);
startTaskHandler.getKey();

嗯嗯,所以,我用的就是ioc.get(Z.class)这样子,获取bean呢,但是有时启动,获取失败

我说的不要在属性里面ioc.get!!!

我是作为入参的值,这样有什么错的吗

CommonContext.ioc = config.getIoc();
X.register(CommonContext.ioc.get(z.class));

没有, Setup.init方法内的ioc.get没问题

看了nutz的启动的日志,是有加载到Z的:

2017-09-19 15:01:14,045 [localhost-startStop-1] INFO  org.nutz.mvc.impl.NutLoading.load(NutLoading.java:56) Nutz.Mvc[nutz] is initializing ...
2017-09-19 15:01:14,890 [localhost-startStop-1] INFO  org.nutz.ioc.loader.annotation.AnnotationIocLoader.<init>(AnnotationIocLoader.java:50) Found 23 classes in 1 base-packages!
beans = ["Z", "Y", "H"....]
2017-09-19 15:01:14,902 [localhost-startStop-1] INFO  org.nutz.ioc.impl.NutIoc.<init>(NutIoc.java:102) NutIoc init begin ...

但是,就是以下的代码,大多数不报错,偶尔报错

CommonContext.ioc = config.getIoc();
X.register(CommonContext.ioc.get(z.class));

发报错的完整日志

2017-09-19 10:12:38,807 [localhost-startStop-1] ERROR x.x.x.x.MvcSetup.init(MvcSetup.java:72) MvcSetup|init|fail|"errorMessage"="[z] # FAIL to create Ioc Bean name=[z]"
org.nutz.ioc.IocException: [z] # FAIL to create Ioc Bean name=[z]
        at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:150)
        at x.x.x.x.MvcSetup.initMessageHandlerAndReceiver(MvcSetup.java:100)
        at x.x.x.x.MvcSetup.init(MvcSetup.java:64)
        at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:276)
        at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:120)
        at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
        at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
        at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
        at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4958)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5652)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
Caused by: org.nutz.lang.born.BorningException: Fail to born 'x.x.x.x.x.Z' becasue: java.lang.NullPointerException
        at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:19)
        at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
        at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
        ... 26 more
Caused by: java.lang.NullPointerException
        at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:55)
        at org.nutz.ioc.ObjectProxy.get(ObjectProxy.java:69)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:150)

加上前面10行日志

我隐约感觉到是某个老bug

2017-09-19 10:12:32,965 [localhost-startStop-1] INFO  org.nutz.mvc.impl.NutLoading.load(NutLoading.java:55) Nutz Version : 1.r.57
2017-09-19 10:12:32,965 [localhost-startStop-1] INFO  org.nutz.mvc.impl.NutLoading.load(NutLoading.java:56) Nutz.Mvc[nutz] is initializing ...
2017-09-19 10:12:34,615 [localhost-startStop-1] INFO  org.nutz.ioc.loader.annotation.AnnotationIocLoader.<init>(AnnotationIocLoader.java:50) Found 23 classes in 1 base-packages!
beans = ["z", "Y"...]
2017-09-19 10:12:34,624 [localhost-startStop-1] INFO  org.nutz.ioc.impl.NutIoc.<init>(NutIoc.java:102) NutIoc init begin ...
2017-09-19 10:12:34,640 [localhost-startStop-1] INFO  org.nutz.ioc.impl.NutIoc.<init>(NutIoc.java:119) ... NutIoc init complete
2017-09-19 10:12:34,646 [localhost-startStop-1] INFO  org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:158) Build URL mapping by org.nutz.mvc.impl.UrlMappingImpl ...
2017-09-19 10:12:34,691 [localhost-startStop-1] WARN  jetbrick.template.web.JetWebEngine.doCreateWebEngine(JetWebEngine.java:100) no default config file found: classpath:/jetbrick-template.properties
2017-09-19 10:12:35,233 [localhost-startStop-1] INFO  org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:23) Init file-pool by: ~/nutz/blog/upload/tmps [1000]
2017-09-19 10:12:35,308 [localhost-startStop-1] INFO  org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:66) file-pool.cursor: 1000
2017-09-19 10:12:35,460 [localhost-startStop-1] WARN  org.nutz.mvc.impl.processor.ViewProcessor.init(ViewProcessor.java:29) Not a good idea : Return String ,and using JsonView!! (Using @Ok("raw") or return map/list/pojo)--> SmsAction.renderTemplate(...)
2017-09-19 10:12:35,503 [localhost-startStop-1] INFO  org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:219) Found 33 module methods
2017-09-19 10:12:35,517 [localhost-startStop-1] INFO  org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:273) Setup application...
2017-09-19 10:12:35,519 [localhost-startStop-1] INFO  com.asiainfo.womail.nc.api.MvcSetup.init(MvcSetup.java:45) MvcSetup|init|processing
2017-09-19 10:12:36,091 [localhost-startStop-1] INFO  org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:23) Init file-pool by: /opt/project/.nutz/tmp/dao/ [200000]
2017-09-19 10:12:36,092 [localhost-startStop-1] INFO  org.nutz.filepool.NutFilePool.<init>(NutFilePool.java:66) file-pool.cursor: 0
2017-09-19 10:12:36,115 [localhost-startStop-1] INFO  org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:103) Get Connection from DataSource for JdbcExpert, if I lock at here, check your database server and configure

你需要调到DEBUG才能看到了.

不过我猜是这个bug: 存在一个@Inject, 属性名跟bean name不匹配, 然后呢,需要遍历所有的ioc bean定义(IocObject)来搜索合适的对象, 然后呢, 某个IocObject的type是null,触发这个bug

要不你升级一下试试, 用58或59

如果不打算升级, 可以找找 @Inject的引用, 逐个改成下面的形式, 看看是哪个@Inject属性的问题

@Inject("refer:具体的名字")

嗯嗯,那我添加下日志,打印看看,是不是这个问题

调试到,发现不是这个问题呢,所有的IocObject的type都有值,但是,偶尔还是会报这个错
升级看看了

        at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:55)
        at org.nutz.ioc.ObjectProxy.get(ObjectProxy.java:69)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265)
        at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:150)

看上去是引用了很多层之后的才是null

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