NutzCN Logo
问答 怎么回事哦??环境准备就出问题了???
发布于 2581天前 作者 苍蓝猛兽 1560 次浏览 复制 上一个帖子 下一个帖子
标签:

创建maven项目,nutz版本1.r.63

src/main/resources/db.js

var ioc = {
	// 读取配置文件
	conf : {
		type : "org.nutz.ioc.impl.PropertiesProxy",
		fields : {
			paths : [ "etc/" ]
		}
	},
	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" } ]
	}
};

etc目录下有db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1/quickpoi?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
db.username=root
db.password=123456

IocMaster

package com.elvish.quickpoi.base.nutz;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.combo.ComboIocLoader;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * Ioc管理容器
 * 
 * @author Elvish
 */
public class IocMaster {

	private static final Log LOGGER = Logs.get();

	private static Ioc ioc = null;

	private IocMaster() {
	}

	public static Ioc getInstance() {
		if (null != ioc) {
			return ioc;
		}

		try {
			ioc = new NutIoc(new ComboIocLoader("*js", ".", "*anno",
					"com.elvish.quickpoi", "*tx"));
		} catch (ClassNotFoundException e) {
			String msg = "create ioc failed...";
			LOGGER.error(msg);
			LOGGER.error(e.getMessage());
			throw new RuntimeException(e);
		}
		return ioc;
	}

	public static <T> T get(Class<T> clz) {
		return getInstance().get(clz);
	}

	public static <T> T get(Class<T> clz, String name) {
		return getInstance().get(clz, name);
	}

}

日志

17-11-28 11:10:28.641 INFO [main] Select SystemLog as Nutz.Log implement
17-11-28 11:10:28.829 DEBUG [main] Locations count=20 time use 65ms
17-11-28 11:10:28.913 DEBUG [main] Found 0 resource by src( . ) , regex( ^(.+[.])(js|json)$ )
17-11-28 11:10:28.955 DEBUG [main] Using 95 castor for Castors

疑问

ioc = new NutIoc(new ComboIocLoader("*js", ".", "*anno","com.elvish.quickpoi", "*tx"));

上面代码中位置给错了??? 试过不用“.”,用“”也是同样找不到文件。

6 回复

不要放在根目录下, 放到 src/main/resources/ioc/db.js

ioc = new NutIoc(new ComboIocLoader("*js", "ioc/", "*anno", "com.elvish.quickpoi", "*tx"));

@wendal

IocMaster

package com.elvish.quickpoi.base.nutz;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.combo.ComboIocLoader;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * Ioc管理容器
 * 
 * @author Elvish
 */
public class IocMaster {

	private static final Log LOGGER = Logs.get();

	private static Ioc ioc = null;

	private IocMaster() {
	}

	public static Ioc getInstance() {
		if (null != ioc) {
			return ioc;
		}

		try {
			ioc = new NutIoc(new ComboIocLoader("*js", "ioc/", "*anno",
					"com.elvish.quickpoi", "*tx"));
		} catch (ClassNotFoundException e) {
			String msg = "create ioc failed...";
			LOGGER.error(msg);
			LOGGER.error(e.getMessage());
			throw new RuntimeException(e);
		}
		return ioc;
	}

	public static <T> T get(Class<T> clz) {
		return getInstance().get(clz);
	}

	public static <T> T get(Class<T> clz, String name) {
		return getInstance().get(clz, name);
	}

}

日志

17-11-28 11:20:44.464 INFO [main] Select SystemLog as Nutz.Log implement
17-11-28 11:20:44.665 DEBUG [main] Locations count=20 time use 74ms
17-11-28 11:20:44.674 DEBUG [main] Found 0 resource by src( ioc/ ) , regex( ^(.+[.])(js|json)$ )
17-11-28 11:20:44.733 DEBUG [main] Using 95 castor for Castors

结论

还是找不到文件

难道是pom.xml里面排除了???

是eclipse吗? resources目录的图标与java目录一样吗(判断是不是设置为源文件夹了)?

@wendal @Wizzercn

换IDEA 操作

目录结构,代码都与上面相同,可以找到js文件了。

报错日志

org.nutz.castor.FailToCastObjectException: Fail to cast from <java.lang.String> to <java.sql.Driver> for {com.mysql.jdbc.Driver}
	at org.nutz.castor.Castors.cast(Castors.java:262)
	at org.nutz.castor.Castors.castTo(Castors.java:316)
	at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:46)
	at org.nutz.ioc.impl.PropertiesProxy.make(PropertiesProxy.java:376)
	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.lang.Invoking$DefaultInvoker.invoke(Invoking.java:47)
	at org.nutz.lang.Invoking.invoke(Invoking.java:192)
	at org.nutz.lang.Mirror.invoke(Mirror.java:1120)
	at org.nutz.ioc.impl.ObjectMakerImpl$1.born(ObjectMakerImpl.java:78)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:115)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:216)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:62)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:216)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:245)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:152)
	at com.elvish.quickpoi.base.nutz.IocMaster.get(IocMaster.java:39)
	at com.elvish.quickpoi.QuickpoiTest.setUp(QuickpoiTest.java:27)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'java.sql.Driver'
 by args: [@(com.mysql.jdbc.Driver)]
 because: No suitable Constructor or Factory Method!!
	at org.nutz.lang.Mirror.born(Mirror.java:1008)
	at org.nutz.castor.castor.String2Object.cast(String2Object.java:16)
	at org.nutz.castor.castor.String2Object.cast(String2Object.java:9)
	at org.nutz.castor.Castors.cast(Castors.java:252)
	... 43 more

db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1/quickpoi?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
db.username=root
db.password=123456

db.js

var ioc = {
    // 读取配置文件
    conf : {
        type : "org.nutz.ioc.impl.PropertiesProxy",
        fields : {
            paths : [ "etc/" ]
        }
    },
    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" } ,
            {refer : "fileSqlManager" }
        ]
    },
    fileSqlManager : {
        type : "org.nutz.dao.impl.FileSqlManager",
        args : "sql"
    }
}

去掉db.driver

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