NutzCN Logo
问答 在module加入@inject Dao dao ;访问报错
发布于 2539天前 作者 yuffo 1944 次浏览 复制 上一个帖子 下一个帖子
标签:

加入 下面代码到子模块

@Inject
private Dao dao;

然后随便访问哪个入口 就报错

@Api("time")
@At("/time")
@IocBean(create="init", depose="depose")
public class TimeModule {
    
    @Inject
    protected PropertiesProxy conf;
    @Inject
    private Dao dao;
    
    @ApiOperation(value = "获取当前毫秒数", notes = "服务器端的时间", httpMethod="GET", response=Long.class)
    @At
    @Ok("raw")
    public long now() {
        return System.currentTimeMillis();
    }
    
    public void init() {}
    public void depose() {}

}

ioc.js

var ioc={
    conf : {
        type : "org.nutz.ioc.impl.PropertiesProxy",
        fields : {
            paths : ["db.properties"]
        }
    },
	fileSqlManager : {
		type: "org.nutz.dao.impl.FileSqlManager"
		args: ["sqls"]
	},
	dataSource : {
		type : "org.nutz.dao.impl.SimpleDataSource",
		events : {
			depose : "close"
		},
		fields : {
			driverClassName :{	java : "$conf.get('db-driver')"},
			jdbcUrl             : {java :"$conf.get('db-url')"},
			username        : {java :"$conf.get('db-username')"},
			password        : {java :"$conf.get('db-password')"},
		}
	}
	,
	dao : {
		type : 'org.nutz.dao.impl.NutDao',
		args : [ {refer : "dataSource"} ,{refer:"fileSqlManager"}]
	}}

但是单元测试可以通过,还能用dao查询

[INFO ] 15:12:08.461 com.boyuan.MainLauncher.init(MainLauncher.java:29) - 服务初始化完毕
[INFO ] 15:12:08.462 org.nutz.boot.NbApp._run(NbApp.java:196) - NB started : 1483ms
[DEBUG] 15:12:18.260 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [GET] path=/queryAll : MapModule.queryAll(MapModule.java:24)
[DEBUG] 15:12:18.261 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:163) - Get 'mapModule'<class com.boyuan.module.MapModule>
[DEBUG] 15:12:18.262 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:189) - 	 >> Load definition name=mapModule
[DEBUG] 15:12:18.262 org.nutz.ioc.loader.combo.ComboIocLoader.printFoundIocBean(ComboIocLoader.java:197) - Found IocObject(mapModule) in AnnotationIocLoader(packages=[com.boyuan])
[DEBUG] 15:12:18.262 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:220) - 	 >> Make...'mapModule'<class com.boyuan.module.MapModule>
[DEBUG] 15:12:18.262 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'mapModule' to [app] 
[DEBUG] 15:12:18.263 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class com.boyuan.module.MapModule without AOP
[DEBUG] 15:12:18.265 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:163) - Get 'dao'<interface org.nutz.dao.Dao>
[DEBUG] 15:12:18.265 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:189) - 	 >> Load definition name=dao
[DEBUG] 15:12:18.265 org.nutz.ioc.loader.combo.ComboIocLoader.printFoundIocBean(ComboIocLoader.java:197) - Found IocObject(dao) in JsonLoader(paths=[ioc/])
[DEBUG] 15:12:18.265 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:220) - 	 >> Make...'dao'<interface org.nutz.dao.Dao>
[DEBUG] 15:12:18.265 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'dao' to [app] 
[DEBUG] 15:12:18.266 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:163) - Get 'dataSource'<>
[DEBUG] 15:12:18.266 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:189) - 	 >> Load definition name=dataSource
[DEBUG] 15:12:18.267 org.nutz.ioc.loader.combo.ComboIocLoader.printFoundIocBean(ComboIocLoader.java:197) - Found IocObject(dataSource) in JsonLoader(paths=[ioc/])
[DEBUG] 15:12:18.267 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:220) - 	 >> Make...'dataSource'<>
[DEBUG] 15:12:18.267 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'dataSource' to [app] 
[DEBUG] 15:12:18.268 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class org.nutz.dao.impl.SimpleDataSource without AOP
[WARN ] 15:12:18.487 org.nutz.dao.impl.SimpleDataSource.<init>(SimpleDataSource.java:34) - SimpleDataSource is NOT a Connection Pool, So it is slow but safe for debug/study
[DEBUG] 15:12:18.493 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:163) - Get 'conf'<>
[INFO ] 15:12:18.494 org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:61) - Fail to value by setter
java.lang.NullPointerException
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at org.nutz.lang.Lang.loadClass(Lang.java:2074)
	at org.nutz.dao.impl.SimpleDataSource.setDriverClassName(SimpleDataSource.java:52)
	at org.nutz.dao.impl.SimpleDataSource$FM$setDriverClassName$ab706b0cf40b3b24094a0af312e173b9.invoke(SimpleDataSource.java)
	at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:51)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:33)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:238)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:64)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:238)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:238)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:268)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:530)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
	at java.lang.Thread.run(Thread.java:748)
[DEBUG] 15:12:18.496 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) - Remove object 'dataSource' from [app] 
[DEBUG] 15:12:18.496 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) - Remove object 'dao' from [app] 
[DEBUG] 15:12:18.496 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) - Remove object 'mapModule' from [app] 
[WARN ] 15:12:18.497 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/queryAll :
org.nutz.ioc.IocException: IocBean[mapModule] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:238)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:268)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:530)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: IocBean[mapModule] fail at field=[dao]
	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)
	... 44 more
Caused by: org.nutz.ioc.IocException: IocBean[dao -> dataSource] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:238)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:64)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:238)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	... 46 more
Caused by: java.lang.RuntimeException: IocBean[dataSource] fail at field=[driverClassName]
	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)
	... 52 more
Caused by: java.lang.RuntimeException: Fail to set 'null'[ null ] by setter org.nutz.dao.impl.SimpleDataSource.'setDriverClassName()' because [java.lang.NullPointerException]: null
	at org.nutz.lang.Lang.wrapThrow(Lang.java:168)
	at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:62)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:33)
	... 54 more
Caused by: java.lang.NullPointerException
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at org.nutz.lang.Lang.loadClass(Lang.java:2074)
	at org.nutz.dao.impl.SimpleDataSource.setDriverClassName(SimpleDataSource.java:52)
	at org.nutz.dao.impl.SimpleDataSource$FM$setDriverClassName$ab706b0cf40b3b24094a0af312e173b9.invoke(SimpleDataSource.java)
	at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:51)
	... 55 more
8 回复

db.properties

db-driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
db-url=jdbc:sqlserver://192.168.0.4;DatabaseName=test
db-username=sa
db-password=******

这是nutzboot??

再把主模块贴一下

@IocBean(create = "init", depose = "depose")
@IocBy(args = { "*js", "ioc/", "*anno", "com.boyuan", "*tx", "*async", "48", })
@Modules(scanPackage = true) 
public class MainLauncher {
	private static final Log log = Logs.getLog(MainLauncher.class);
	@Inject
	protected PropertiesProxy conf;

	@At("/")
	@Ok("->:/index.html")
	public void index() {
	}

	public void init() {
		// NB自身初始化完成后会调用这个方法
		log.info("服务初始化完毕");
	}

	public void depose() {
		log.info("服务结束");
	}

	public static void main(String[] args) throws Exception {
		new NbApp().setArgs(args).setPrintProcDoc(true).run();
	}

}

这是nutzboot呢, 除非那个main方法是忘记删掉了

嗯.从NB生成器生成的,nutzboot 单元测试能查的 但是启动服务再访问就报错了

既然是nutzboot项目, 按nutzboot的规则做, 配置信息是放在application.properties里面的, dao.js是多余的

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