NutzCN Logo
问答 ioc 注入文件 部署报错
发布于 2533天前 作者 tiankongkm 1737 次浏览 复制 上一个帖子 下一个帖子
标签:
shardingDataSource : {
	        type : "com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource",
	        args : [{file:"sharding-jdbc.yaml"}]   
	    }
because:java.io.FileNotFoundException: file:\D:\git\vedio\vedio-parent\vedio-core\target\vedio-core-0.0.1-SNAPSHOT-jar-with-dependencies.jar!\sharding-jdbc.yaml (文件名、目录名或卷标语法不正确。)
        at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:25)
        at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
        at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
        ... 10 more
Caused by: java.io.FileNotFoundException: file:\D:\git\vedio\vedio-parent\vedio-core\target\vedio-core-0.0.1-SNAPSHOT-jar-with-dependencies.jar!\sharding-jdbc.yaml (文件名、目录名或卷标语法不正确。)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource.unmarshal(YamlShardingDataSource.java:56)
        at com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource.<init>(YamlShardingDataSource.java:43)
        at com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource$FC$33c11f0522e62aeabcf71f001c8949a2.invoke(YamlShardingDataSource.java)
        at org.nutz.lang.born.AbstractConstructorBorning.call(AbstractConstructorBorning.java:25)
        at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:19)
        ... 12 more

eclipse启动没问题,cmd jar 启动就报错了

9 回复

走注解式ioc吧

@IocBean
public class MyBeans {
 
    @IocBean(name="dataSource")
    public YamlShardingDataSource createDataSource(){
        return new YamlShardingDataSource(getClass().getResourceAsStream(.....));

    }
}

@wendal

File file = new File("sharding-jdbc.yaml");
    	Files.write(file,getClass().getResourceAsStream("sharding-jdbc.yaml"));
    	return new YamlShardingDataSource(file);
Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.tiankong.core.Server.dbInit(Server.java:76)
	at com.tiankong.core.Server.init(Server.java:61)
	at com.tiankong.core.DubboStart.main(DubboStart.java:24)
Caused by: org.nutz.ioc.IocException: IocBean[shardingDao -> shardingDataSource] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:150)
	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 com.tiankong.core.db.DB.<init>(DB.java:49)
	at com.tiankong.core.db.DB.<clinit>(DB.java:21)
	... 3 more
Caused by: org.nutz.lang.InvokingException: Fail to invoke [com.tiankong.core.ioc.MyBeans].createDataSource() by args:
 
For the reason: sharding-jdbc.yaml (系统找不到指定的文件。)
	at org.nutz.lang.Invoking.invoke(Invoking.java:195)
	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)
	... 10 more
Caused by: java.io.FileNotFoundException: sharding-jdbc.yaml (系统找不到指定的文件。)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource.unmarshal(YamlShardingDataSource.java:56)
	at com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource.<init>(YamlShardingDataSource.java:43)
	at com.tiankong.core.ioc.MyBeans.createDataSource(MyBeans.java:18)
	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$NullArgInvoker.invoke(Invoking.java:77)
	at org.nutz.lang.Invoking.invoke(Invoking.java:192)
	... 14 more

请问大佬怎么转file对象

getClass.getClassLoader.getResourceStream

nu不要涉及到File类了

maven项目?是放在resources下面的吧?

@wendal 嗯嗯,maven项目 是在resources下面

那用getClass.getClassLoader.getResourceStream
就好了,好像能接受InputStream的吧,如果不能,Streams类有帮助方法变成字符串

@wendal 谢谢 解决了,能接受字节数组

   public YamlShardingDataSource createDataSource() throws IOException, SQLException{
    	return new YamlShardingDataSource("sharding-jdbc",Streams.readBytes(getClass().getClassLoader().getResourceAsStream("sharding-jdbc.yaml")));
    }

好的,报个issue吧

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