NutzCN Logo
问答 nutz采用Druid的问题
发布于 2386天前 作者 qq_1b1521b4 2245 次浏览 复制 上一个帖子 下一个帖子
标签:

POM文件:

<!-- nutz -->
		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz</artifactId>
			<version>1.r.65<ersion>
		</dependency>
<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.10</ version>
		</dependency>
<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.11</ version>
			<scope>runtime</scope>
		</dependency>

调用程序:
Ioc ioc = new NutIoc(new JsonLoader("/conf/ioc/ dao.js"));
DataSource ds = ioc.get(DataSource.class);
报错:

Exception in thread "main" org.nutz.ioc.IocException: IocBean[dataSource] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:160)
	at cn.tba.pojo.Person.main(Person.java:62)
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'javax.sql.DataSource'
 by args: []
 because: No suitable Constructor or Factory Method!!
	at org.nutz.lang.Mirror.getBorning(Mirror.java:967)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:102)
	... 4 more

若调用程序改为:
Ioc ioc = new NutIoc(new JsonLoader("/conf/ioc/dao.js"));
DataSource ds = ioc.get(DruidDataSource.class);
报错为:

Exception in thread "main" org.nutz.ioc.IocException: IocBean[druidDataSource] For object [druidDataSource] - type:[class com.alibaba.druid.pool.DruidDataSource]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:249)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:160)
	at cn.tba.pojo.Person.main(Person.java:62)
Caused by: org.nutz.ioc.ObjectLoadException: Object 'druidDataSource' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:157)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:194)
	... 3 more
6 回复

完整启动日志发一下

另外,这是做测试吗? nutz mvc下不需要新建NutIoc实例的

是做测试
Locations count=46 time use 75ms
Found 1 resource by src( /conf/ioc/dao.js ) , regex( ^(.+[.])(js|json)$ )
loading [dao.js]
Loaded 1 bean define from path=[/conf/ioc/dao.js] --> [dataSource]
... NutIoc init complete
Get 'dataSource'<interface javax.sql.DataSource>
Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
>> Load definition name=dataSource
Found IocObject(dataSource) in JsonLoader(paths=[/conf/ioc/dao.js])
>> Make...'dataSource'<interface javax.sql.DataSource>
Save object 'dataSource' to [app]
Remove object 'dataSource' from [app]
Exception in thread "main" org.nutz.ioc.IocException: IocBean[dataSource] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:160)
at cn.tba.pojo.Person.main(Person.java:52)
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'javax.sql.DataSource'
by args: []
because: No suitable Constructor or Factory Method!!
at org.nutz.lang.Mirror.getBorning(Mirror.java:967)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:102)
... 4 more

dao.js 贴一下

var ioc = {
	dataSource : {
		type : "com.alibaba.druid.pool.DruidDataSource",
		events : {
			create : "init",
			depose : 'close'
		},
		fields : {
			driverClassName:"com.mysql.cj.jdbc.Driver",
			url : "jdbc:mysql://localhost:3306/nutz?serverTimezone=Asia/Shanghai",
			username : "root",
			password : "root",
			testWhileIdle : true, // 非常重要,预防mysql的8小时timeout问题
			maxWait : 15000, // 若不配置此项,如果数据库未启动,druid会一直等可用连接,卡住启动过程
			maxActive : 100
			defaultAutoCommit : false
		// 提高fastInsert的性能
		}
		dao:{
			type:"org.nutz.dao.impl.NutDao",
			args:[{refer:"dataSource"}]
		}
	}
}

dao怎么在dataSource里面?

改过后可以了,万分谢谢!:)

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