【发布时间】:2017-06-04 03:32:57
【问题描述】:
我是 Spring 和 Maven 的新手。我正在尝试使用 Maven 原型构建一个 Spring 应用程序。所以我用 maven pom.xml 设置了项目。一切正常,但唯一的问题是我的 IDE(netbeans)没有在类路径中包含我的 spring.xml。结果,构建失败,找不到文件异常。我必须专门给出 spring.xml 的完全限定路径才能使其正常工作。
所以下面的失败 -
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
如果我按以下方式更改它,它会起作用 -
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:///Users/shoukat/NetBeansProjects/csm/src/main/java/spring.xml");
我发现这是因为我的类路径不包含 main/java 目录。以下是我尝试使用以下代码打印类加载器 URL 时的结果-
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
//Get the URLs
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< urls.length; i++)
{
System.out.println(urls[i].getFile());
}
结果 -
/Users/shoukat/NetBeansProjects/csm/target/classes/
/Users/shoukat/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar
/Users/shoukat/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
/Users/shoukat/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.5/jcl-over-slf4j-1.7.5.jar
/Users/shoukat/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar
/Users/shoukat/.m2/repository/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0.0.RELEASE.jar
/Users/shoukat/.m2/repository/org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar
/Users/shoukat/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar
/Users/shoukat/.m2/repository/org/springframework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar
/Users/shoukat/.m2/repository/org/springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar
理想情况下,这也应该包括 src 目录,因为我将资源 spring.xml 文件放在那里。我需要更改什么,以便我的 src/main 目录包含在类路径中,并且我不必使用 spring.xml 文件的完全限定系统路径对我的程序进行硬编码?
谢谢
【问题讨论】: