2017-06-04 14 views
1

私はSpringとMavenが初めてです。私は、Mavenのアーキタイプを使用して春のアプリケーションを構築しようとしています。だから私はmaven pom.xmlでプロジェクトをセットアップしました。すべてうまく動作しますが、唯一の問題はIDE(netbeans)がclasspathにspring.xmlを含めていないことです。結果として、ファイルが見つかりませんでした例外でビルドが失敗します。私は具体的にspring.xmlの完全修飾パスを与える必要があります。Mavenクラスパスにリソースを追加する方法

ので、下記の失敗 - AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

をそれが動作する以下のように私はそれを変更した場合 -

AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:///Users/shoukat/NetBeansProjects/csm/src/main/java/spring.xml"); 

私は私のクラスパスは、メイン/ 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ディレクトリを含める必要があります.xmlファイルがそこにあります。 my src/mainディレクトリがクラスパスに含まれているため、私のプログラムをspring.xmlファイルの完全修飾システムパスでハードコードする必要はありません。

おかげ

答えて

2

コンパイルはこのためsrc/main/java

に入れ* .xmlファイルなどのリソースを無視したときに、あなたのクラスパスが、Mavenの中01​​検索spring.xmlを使用して、リソースのフォルダやフォルダを作成する必要が動作するようにツリーは次のようである:

src/main/java/.. 
src/main/resources/spring.xml 

次にあなたが使用することができます。

AbstractApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml");

Maven - Introduction to the Standard Directory Layout

関連する問題