Spring import resource: Skip if file does not exist - spring

I added this to my Spring configuration inside my WAR file:
<import resource="classpath:myapp-custom-settings.xml"/>
This way, users can create a myapp-custom-settings.xml file (in a folder listed in shared.loader) to modify the beans they want without having to unzip the WAR.
Very convenient, but most users don't have such a file, in which case a FileNotFoundException happens and the app does not start:
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:Configuration problem: Failed to import bean definitions from URL location [file:/home/nico/tomcat/conf/myapp-custom-settings.xml]
Offending resource: ServletContext resource [/WEB-INF/spring/root-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:/home/nico/tomcat/conf/myapp-custom-settings.xml]; nested exception is
java.io.FileNotFoundException: /home/nico/tomcat/conf/myapp-custom-settings.xml (No such file or directory)
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:233)
QUESTION: Is there a way to make this import failsafe?
I mean, load the file if it exists, but skip if it does not exist.

The trick is to use classpath* instead of classpath. Note the * asterisk character.
Example:
<import resource="classpath*:myapp-custom-settings.xml"/>
The app will now start normally even if no myapp-custom-settings.xml file is found.

Related

Getting error in trying to read applicationContext.xml in Standalone java

ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
InvGroupDelegate igcd = (InvGroupDelegate) context.getBean("invGroupDelegate");
Giving error:
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
The appContext file is present in myProject/properties
And this is also added to 'Src folders in the build path'.
Resolved it. Was an eclipse issue. Closed the project and reopned it. Then cleaned up everything and built once more. Then things were fine.

Cannot load resource in spring-data project

I am using spring-data project in combination with hadoop to run map-red jobs. My application context seems not to resolve classpath resources.
<hdp:job id="myjob" input-path="wordcount/in/" output-path="wordcount/outtttt/"
mapper="org.apache.hadoop.examples.WordCount.TokenizerMapper"
reducer="org.apache.hadoop.examples.WordCount.IntSumReducer"
jar="classpath:hadoop-examples-1.0-SNAPSHOT.jar"
jar-by-class="org.apache.hadoop.examples.WordCount"/>
<hdp:job-runner id="myjob-runner" job-ref="myjob" run-at-startup="true"/>
I see from my classpath when I run a program that jar is present:
-classpath "C:\Program Files\Java\jdk1.7.0_25\jre\lib\charsets.jar;... atd;C:\Users\JS185350\WorkSpace\HBasePlaying\hadoop-examples\target\hadoop-examples-1.0-SNAPSHOT.jar;
Error creating bean with name 'myjob': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classpath:hadoop-examples-1.0-SNAPSHOT.jar] cannot be resolved to URL because it does not exist
Any guess what might be wrong?
try adding a slash / in front of the hadoop-examples...
<hdp:job id="myjob" input-path="wordcount/in/" output-path="wordcount/outtttt/"
mapper="org.apache.hadoop.examples.WordCount.TokenizerMapper"
reducer="org.apache.hadoop.examples.WordCount.IntSumReducer"
jar="classpath:/hadoop-examples-1.0-SNAPSHOT.jar"
jar-by-class="org.apache.hadoop.examples.WordCount"/>
<hdp:job-runner id="myjob-runner" job-ref="myjob" run-at-startup="true"/>

FileNotFound exception thrown when trying to create application-context object using ClassPathXmlApplicationContext on Weblogic server

Scenario
There was a requirement in my project to multi-thread calls made to a Webservice. This is implemented using ThreadPoolTaskExecutor (Spring).
As the thread class required values to be fed at runtime, had implemented the code as below -
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
for (DocumentData documentData : documentDataList){
MinerThread minerThread = applicationContext.getBean("minerThread",MinerThread.class);
minerThread.setHeader(header);
minerThread.setDocumentData(documentData);
minerThread.setBatchSize(batchSize);
minerThreadExecutor.execute(minerThread);
}
Issue
In local I have tomcat server and this implementation works as expected. However on Weblogic (on the client machine, I don't have access to Weblogic) when the war is deployed it throws FileNotFound Exception.
As the application context object is created via ClassPathXmlApplicationContext API thought the error was thrown because the location was not under the Weblogic classpath. There was nothing set in the Weblogic server classpath. However even after including WEB-INF/classes in the classpath the same exception is thrown.
Exception
2013/01/29 09:19:12,853 [ERROR] RequestQueueListener - org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../shared-common-context.xml]
Offending resource: class path resource [application-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [../shared-common-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [../shared-common-context.xml] cannot be opened because it does not exist
BeanDefinitionParsingException is caused because of the relative path that was added in the Weblogic classpath.
On the Weblogic the file is at the location -
./wls10.3.4/ColorisPPT1/user_projects/domains/Coloris_PPT1_Domain/servers/Coloris_PPT1_AdminServer/tmp/.appmergegen_1336142780924_documentservice.war/WEB-INF/classes
When the war if built (Maven is used) the application-context.xml is placed under WEB-INF/classes folder.
Could someone please suggest how to proceed with this.

Spring inside war cannot find classpath resource in an inner jar file

I have a project organized like this:
core
-- /src/main/resources/company/config/spring-config.xml
webapp
-- /WEB-INF/applicationContext.xml
The webapp depends on core.jar, which is included correctly in WEB-INF/lib when I deploy.
In web.xml I have:
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
And in applicationContext.xml I have:
<import resource="classpath:/company/config/spring-config.xml" />
But when I run, I get this error:
2012-10-04 20:03:39,156 [localhost-startStop-1] springframework.web.context.ContextLoader ERROR: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/company/config/spring-config.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [company/config/spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
....
Caused by: java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 36 more
When spring-config.xml is in webapp, everything works fine.
I noticed the leading / is removed from some of the errors on the stack trace, and I wonder if this has anything to do with it.
Also, I am (unfortunately) using Spring 2.5, if that matters.
For future reference, I figured out the problem after much debugging. It turns out Eclipse was building my "core" library as a jar, but with a web application package layout, so instead of my resource being located here:
/company/config/spring-config.xml
it was located here:
/WEB-INF/company/config/spring-config.xml
which caused the problem. I had checked the jar a few times before, but had never noticed the sneaky "/WEB-INF" hiding in plain sight.
Removing the project and re-importing it into Eclipse (via the Maven pom.xml file) was not enough to fix the problem.
I had to manually delete the Eclipse-specific .project, .classpath, and .settings files. When I did that and re-imported the project everything was fixed.
The moral of the lesson is: ALWAYS check your resource paths when the exception says "File Not Found".

spring framework Error: Context initialization failed

i am deploying application(spring-hibernate) in tomcat6 but it is giving error-
ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.myPackage.myProject.calendar.bpo.BusinessDirectoryBPOImpl; nested exception is java.io.FileNotFoundException: class path resource [com/myPackage/framework/bpo/BaseBPOImpl.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:180)
......
all jar files are already in \WEB-INF\lib*.jar.
am i missing any class file or any jar file.
how to solve this error.
Thanks
Yugal
com.myPackage.myProject.calendar.bpo.BusinessDirectoryBPOImpl is looking for
com/myPackage/framework/bpo/BaseBPOImpl.class
which it cannot find.
Check for sure if this specific class is present under WEB-INF\lib in a jar or WEB-INF\classes as a class file.

Resources