0

I'm trying to load an application context which is inside a jar as a plugin. I use this to load the context:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**my-context.xml");

When I load the jar through pom.xml, it works fine.

Then I add it directly in the classpath using eclipse instead of maven to avoid to compile every time (ultimate goal is the shared lib folder in tomcat, not working too). Now spring is unable to find it and return a default context (no exception)

I checked that it's correctly insert in the classpath using:

InputStream in1 = this.getClass().getClassLoader().getResourceAsStream("my-context.xml");

It works.

I checked logs. Using the pom.xml, spring is correctly searching in the jar

Searching directory  [...target\classes\META-INF\maven\x.y.z] for files matching pattern [...\x.y.z/target/classes/**/my-context.xml]
Searching directory  [...ehealth.poc.module1] for files matching pattern [D:/JRB/Projects/Vivates/workspaces/default/extcom/ehealth.poc.module1/target/classes/**/ecm-context.xml]
...
Resolved location pattern [classpath*:**/my-context.xml] to resources [file [...\target\classes\my-context.xml]]
Loading XML bean definitions from file [...\target\classes\my-context.xml]
...

In the second case, nothing in the log about my jar.

Why spring does not have the same behavior when I use maven or directly the classpath? I maven doing something else than simple adding dependencies location in the classpath?

Nereis
  • 486
  • 1
  • 4
  • 21
  • you have to add the jar in src/main/resources, this is the default directory to contains the resource that will end up in the classpath – Skizzo Jul 30 '14 at 12:44
  • Not working too. If I just add the jar in the ressources folder, spring doesn't load it and I can't find it anymore using the classloader. – Nereis Jul 31 '14 at 05:51
  • Maven build copies required jars to the lib folder in the war to be sure that they will be available when the war will be deployed. – Serge Ballesta Jul 31 '14 at 06:37
  • In this case, I'm trying to run it in eclipse without deploying it on tomcat. But the goal is to use the shared lib folder of tomcat which doesn't work too. I think the problem is related that why I'm trying first to run only and eclipse to abstract tomcat configuration problems. – Nereis Jul 31 '14 at 08:28
  • @Nereis In your jar in what folder is the file mycontext.xml, To work it should be in src/main/reaources and then you can add the jar like normal dependency – Skizzo Jul 31 '14 at 08:42
  • The jar has been generated using maven then it should have a standard structure. In the current case, the file is at the root of jar and there is no src/main folder. Only META-INF and x/y/z/*.class. Anyway, I tried to put it in META-INF and /resources in the jar. Still not working :S – Nereis Jul 31 '14 at 09:06

1 Answers1

0

Finally, we found the solution on eclipse.

The problem comes from the ** in

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**my-context.xml");

It looks like ** doesn't scan the .jar files. Setting the direct path is working :

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:my-context.xml");
Nereis
  • 486
  • 1
  • 4
  • 21