Reputation: 49
I am new to Spring and Maven. I am trying to build a spring app using maven archetypes. So i have set up the project with maven pom.xml. All things work fine but the only issue is my IDE (netbeans) is not including my spring.xml in the classpath. As a result the build fails with file not found exception. I have to specifically give the fully qualified path of the spring.xml to get this working.
So the below fails -
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
If i change it as below it works -
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:///Users/shoukat/NetBeansProjects/csm/src/main/java/spring.xml");
I find that this is happening because my classpath does not include the main/java directory. Below are the results when i try to print the classloader URLs using the below code-
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());
}
Result -
/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
Ideally this should also include the src directory as i am putting my resources spring.xml file in there. What do i need to change so the my src/main directory is included in classpath and i do not have to hardcode my program with the fully qualified system path of my spring.xml file?
Thanks
Upvotes: 1
Views: 1631
Reputation: 121
When using ClassPathXmlApplicationContext
search spring.xml in your classpath but maven when compile ignore resources like * .xml put in src/main/java
For this to work you have to create the resource folder and the folder tree is like:
src/main/java/..
src/main/resources/spring.xml
Then you can use:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Maven - Introduction to the Standard Directory Layout
Upvotes: 2