談談Java Resources用法

     作Ibatis的配置文件時,用到了Java Resources,把配置文件放置在項目工程中,直接運行始終找不到資源,只能加上幾句Java代碼查看類加載路徑:java

ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();mysql

URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();sql

for (int i = 0; i < urls.length; i++)mybatis

{ui

System.out.println(urls[i].getFile());url

}spa

輸出爲:.net

/D:/wps/club/Mybatis-club/target/test-classes/ xml

/D:/wps/club/Mybatis-club/target/classes/blog

/C:/Users/**/.m2/repository/org/mybatis/mybatis/3.2.2/mybatis-3.2.2.jar

/C:/Users/**/.m2/repository/mysql/mysql-connector-java/5.1.26/mysql-connector-java-5.1.26.jar

/C:/Users/**/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar

把配置文件放到路徑下,便可成功。

若是是用Maven 加上資源拷貝的的功能,如:

<build>

<resources>

<resource>

<directory>src/main/resources</directory>

<filtering>true</filtering>

</resource>

<resource>

<directory>

src/main/java/com/jcloud/mybatis/dao

</directory>

<filtering>true</filtering>

<targetPath>com/jcloud/mybatis/dao</targetPath>

<includes>

<include>**/*.xml</include>

</includes>

</resource>

</resources>

便可!

參考:

http://blog.csdn.net/dongwujing/article/details/7751057

相關文章
相關標籤/搜索