NutzCN Logo
问答 nutzboot自己注入对象报错
发布于 2253天前 作者 文涛(wentao) 1954 次浏览 复制 上一个帖子 下一个帖子
标签:

错误日志:

[DEBUG] 16:45:20.211 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:85) - Remove object 'userUtil' from [app] 
[DEBUG] 16:45:20.212 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:85) - Remove object 'mainLauncher' from [app] 
[ERROR] 16:45:20.214 org.nutz.boot.NbApp.execute(NbApp.java:219) - something happen!!
org.nutz.ioc.IocException: IocBean[mainLauncher] throw Exception when creating
    at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
    at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
    at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
    at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:161)
    at org.nutz.boot.NbApp.execute(NbApp.java:209)
    at org.nutz.boot.NbApp.run(NbApp.java:178)
    at com.tao.MainLauncher.main(MainLauncher.java:41)
Caused by: java.lang.RuntimeException: IocBean[mainLauncher] fail at field=[userUtil]
    at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
    at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
    at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
    ... 6 more
Caused by: org.nutz.ioc.IocException: IocBean[userUtil] throw Exception when creating
    at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
    at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
    at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
    at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
    ... 8 more
Caused by: java.lang.RuntimeException: IocBean[userUtil] fail at field=[aaaRedis]
    at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
    at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
    at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
    ... 11 more
Caused by: org.nutz.ioc.IocException: IocBean[class:com.tao.utils.Redis] none ioc bean match class=com.tao.utils.Redis
    at org.nutz.ioc.impl.NutIoc.getByType(NutIoc.java:461)
    at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:66)
    at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
    ... 13 more

注入代码

/**
 * redis注入类
 *
 * @author wentao
 * @email wentao0291@gmail.com
 * @date 2018-08-08 21:49
 */
@IocBean(create = "createRedis")
public class RedisStarter {

    @Inject
    protected PropertiesProxy conf;

    @Inject("refer:$ioc")
    protected Ioc ioc;

    // @IocBean(name = "redis")
    public void createRedis() {
        // 扫描所有redisName
        String regex = "redis\\.(\\w*)\\.host";
        for (String key : conf.getKeys()) {
            Pattern pattern = Regex.getPattern(regex);
            Matcher match = pattern.matcher(key);
            if (match.find()) {
                // 获取redisName
                String redisName = match.group(1);
                if(!redisName.equals("local")) { // local会在最后默认处理
                    Redis redis = new Redis(redisName, conf);
                    ioc.addBean(redisName + "Redis", redis);
                }
            }
        }
        // return new Redis("local", conf); // 注入默认redis,如果没有配置local,但用户使用了默认redis,则会抛出异常 throw Lang.makeThrow("未连接到Redis,请检查配置项");
    }
}

上面有 @IocBean(name = "redis") return new Redis("local", conf); 被我注解掉了,没注解之前勉强正常
勉强正常的意思是这样

    @Inject
    Redis aaaRedis;

    @Inject
    Redis adRedis;

   // 以上 aaaRedis对象里面为空
    @Inject
    Redis redis;
    @Inject
    Redis aaaRedis;

    @Inject
    Redis adRedis;

    // 以上redis为空  aaaRedis正常
7 回复

你这样写, 需要debug一下createRedis, 看看没有被调用到

debug断点看了下,确实没调用,难道一定要init?

实现一下ServerFace接口,在start方法里面做, 就肯定会调用到了

确实可以了,能否科普下

看来你想深入一下, 把NbApp的org.nutz.boot.NbApp.execute()看一看哦, 知道整个应用的初始化过程

嗯,好的,比较喜欢知其所以然

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