NutzCN Logo
问答 多数据源下获取Dao实例出错
发布于 2433天前 作者 bb3239 1937 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我配置了两个数据库(dao和dao2),配置信息如下:

var ioc = {
		conf : {
			type : "org.nutz.ioc.impl.PropertiesProxy",
			fields : {
				paths : ["config/custom/"]
			}
		},
	    dataSource : {
			factory : "$conf#make",
			args : ["com.alibaba.druid.pool.DruidDataSource", "db."],
	        type : "com.alibaba.druid.pool.DruidDataSource",
	        events : {
	        	create : "init",
	            depose : 'close'
	        }
	    },
		dao : {
			type : "org.nutz.dao.impl.NutDao",
			args : [{refer:"dataSource"}],
			fields : {
				executor : {refer:"cacheExecutor"}
			}
		},
        dataSource2 : {
              factory : "$conf#make",
              args : ["com.alibaba.druid.pool.DruidDataSource", "db2."],
              type : "com.alibaba.druid.pool.DruidDataSource",
               events : {
                          create : "init",
                          depose : 'close'
                         }
        },
        dao2 : {
               type : "org.nutz.dao.impl.NutDao",
               args : [{refer:"dataSource2"}]
        },
		cacheExecutor : {
			type : "org.nutz.plugins.cache.dao.CachedNutDaoExecutor",
			fields : {
				cacheProvider : {refer:"cacheProvider"},
				cachedTableNames : ["sys_user", "sys_role", "sys_menu"]
			}
		},
		cacheProvider : {
			type : "org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider",
			fields : {
				cacheManager : {refer:"cacheManager"} // 引用ehcache.json中定义的CacheManager
			},
			events : {
				create : "init"
			}
		}
};

在SetUp的init方法里面添加如下代码:

Ioc ioc = config.getIoc();
            Dao dao = ioc.get(Dao.class);
            Dao dao2 = ioc.get(Dao.class,"dao2");

dao可以获取到,但是创建dao2的时候出错,错误代码如下:

[DEBUG] 2018-03-19 14:12:33,475 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dao2'<interface org.nutz.dao.Dao>
[DEBUG] 2018-03-19 14:12:33,476 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - 	 >> Load definition name=dao2
[DEBUG] 2018-03-19 14:12:33,476 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) - Loading define for name=dao2
[DEBUG] 2018-03-19 14:12:33,478 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) - Found IocObject(dao2) in JsonLoader(paths=[config/ioc/])
[DEBUG] 2018-03-19 14:12:33,479 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) - 	 >> Make...'dao2'<interface org.nutz.dao.Dao>
[DEBUG] 2018-03-19 14:12:33,479 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) - skip -- org.nutz.dao.impl.NutDao
[DEBUG] 2018-03-19 14:12:33,481 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class org.nutz.dao.impl.NutDao without AOP
[DEBUG] 2018-03-19 14:12:33,482 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'dao2' to [app] 
[DEBUG] 2018-03-19 14:12:33,484 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dataSource2'<>
[DEBUG] 2018-03-19 14:12:33,485 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - 	 >> Load definition name=dataSource2
[DEBUG] 2018-03-19 14:12:33,485 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) - Loading define for name=dataSource2
[DEBUG] 2018-03-19 14:12:33,487 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) - Found IocObject(dataSource2) in JsonLoader(paths=[config/ioc/])
[DEBUG] 2018-03-19 14:12:33,487 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) - 	 >> Make...'dataSource2'<>
[DEBUG] 2018-03-19 14:12:33,490 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) - skip -- com.alibaba.druid.pool.DruidDataSource
[DEBUG] 2018-03-19 14:12:33,495 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class com.alibaba.druid.pool.DruidDataSource without AOP
[DEBUG] 2018-03-19 14:12:33,495 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'dataSource2' to [app] 
[DEBUG] 2018-03-19 14:12:33,496 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<>
[DEBUG] 2018-03-19 14:12:33,504 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) - Remove object 'dataSource2' from [app] 
[DEBUG] 2018-03-19 14:12:33,505 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) - Remove object 'dao2' from [app] 
org.nutz.ioc.IocException: IocBean[dao2 -> dataSource2] throw Exception when creating
[INFO ] 2018-03-19 14:12:44,067 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:141) - Nutz.Mvc[nutz] is up in 18606ms
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:75)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at cn.wizzer.app.web.commons.core.Setup.init(Setup.java:77)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:274)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	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:4700)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1696)
	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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:484)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:433)
	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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
	at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
	at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
	at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
[INFO ] 2018-03-19 14:12:44,067 org.nutz.mvc.NutFilter._init(NutFilter.java:117) - exclusionsPrefix  = ^(/assets/|/druid/|/upload/|/apidoc/)
	at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
	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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
	at sun.rmi.transport.Transport$1.run(Transport.java:200)
	at sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
	at org.apache.catalina.loader.WebappClassLoaderBase.binaryNameToPath(WebappClassLoaderBase.java:2588)
	at org.apache.catalina.loader.WebappClassLoaderBase.findLoadedClass0(WebappClassLoaderBase.java:2756)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1209)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
	at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:559)
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:697)
	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.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	... 59 more
2 回复

db2.url写错了吧

@wendal 是的,谢谢!

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