Cannot read applicationContext.xml when executing spring class as executable jar

I have a spring class with a main method. Inside the class, I am trying to read applicationContext.xml values. My intention is to compress this main class along with its dependent jars, property files, and applicationContext.xml.

But when I try to run the jar file through the unix command line, it looks like the applicationContext file is not loading.

ApplicationContext.xml is displayed inside the jar file and can see sysouts inside my class. The code used to read applicationContext.xml,

ApplicationContext context = new ClassPathXmlApplicationContext(
    "classpath*:**/applicationContext.xml");

When I type context, it gives me the following message.

org.springframework.context.support.ClassPathXmlApplicationContext@89fbe3: start
up date [Mon Oct 01 15:07:43 IST 2012]; root of context hierarchy

When I try to print context.getBeanDefinitionCount () - it gives me 0.

eclipse. applicationContext.xml bean 13.

, , . , .

+5
3

Spring ( ):

, "classpath *:" Ant - , - . , , "classpath *: *. Xml", jar, . JDK ClassLoader.getResources(), ( ).

+2

.jar? Eclipse? , "applicationContext.xml" "/resources". , "Java Build Path" Eclipse. , "/src/main/resources". , XML .jar. , .

+1

. Eclipse, , .

:

  • Going to the build path screen in Eclipse and removing filters in the folder /resources. This causes the export to place the folder META-INFand its contents under /resourcesat the top level of the file .jar.
  • Remove all templates from the application context path:

    applicationContext = new ClassPathXmlApplicationContext( "classpath*:META-INF/spring/applicationContext*.xml");
    becomes
     applicationContext = new ClassPathXmlApplicationContext("classpath:META-INF/spring/applicationContext.xml");

I am sure this is due to some difference in the class loader between the Eclipse environment and the Java java environment. Finding out how to solve this problem will be the next task.

-1
source

All Articles