Could not open ServletContext resource - java

This is quite similar question to one older but the solution did not work for me.
I have a WAR package.
In web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
In application-context.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:social.properties</value>
</property>
</bean>
But getting this:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]
I checked the WAR package - .xml and .properties files are both in /WEB-INF/classes
.properties file is in src/main/resources and .xml in src/main/java (in default package both) and maven transports them (I think) correctly in the default package of WEB-INF/classes
Does anyone know why i could get this exception? Thank you.
EDIT: I just want to add that JUnit tests goes correctly (i mean they load what they should from social.properties) but when running the app it ignores my classpath: prefix

Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
and
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/social.properties</value>
</property>
</bean>

Put the things like /src/main/resources/foo/bar.properties and then reference them as classpath:/foo/bar.properties.

Try to use classpath*: prefix instead.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards
Also please try to deploy exploded war, to ensure that all files are there.

I think currently the application-context.xml file is into src/main/resources AND the social.properties file is into src/main/java... so when you package (mvn package) or when you run tomcat (mvn tomcat:run) your social.properties disappeared (I know you said when you checked into the .war the files are here... but your exception says the opposite).
The solution is simply to put all your configuration files (application-context.xml and social.properties) into src/main/resources to follow the maven standard structure.

Are you having Tomcat unpack the WAR file? It seems that the files cannot be found on the classpath when a WAR file is loaded and it is not being unpacked.

try with this code...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>/social.properties</value>
</list>
</property>
</bean>

Mark sure propertie file is in "/WEB-INF/classes" try to use
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/social.properties</value>
</property>
</bean>

I had the same error.
My filename was jpaContext.xml and it was placed in src/main/resources. I specified param value="classpath:/jpaContext.xml".
Finally I renamed the file to applicationContext.xml and moved it to the WEB-INF directory and changed param value to /WEB-INF/applicationContext.xml, then it worked!

Related

Placing files under WEB-INF directory and reading them

We are using JBOSS 5.1.0.GA and spring integration framework. We are placing the configuration files under the conf directory of the JBOSS to read them from the classpath. But now we are told that we should move all the configuration files from the conf directory to the WEB-INF directory of the war file. Everything was working fine When we placed the files under conf directory.
<bean id="xyz" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:CustomerService/property-files/*.properties</value>
</list>
</property>
</bean>
But when we move the configuration files from conf directory to WEB-INF directory by making the following changes we are getting the Exceptionjava.io.FileNotFoundException.
<bean id="xyz" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/CustomerService/property-files/*.properties</value>
</list>
</property>
</bean>
The exception details:
java.io.FileNotFoundException: URL [jndi:/localhost/pqawdTestWebApp/WEB-INF/CustomerService/spring-integration/Jobs/] cannot be resolved to absolute file path because it does not reside in the file system: jndi:/localhost/pqawdTestWebApp/WEB-INF/CustomerService/spring-integration/Jobs/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:169)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:526)
Anybody has idea on what to do?
Place them in the class path (by some build means).
/WEB-INF/classes/CustomerService/property-files/*.properties
WEB-INF directory path will not be available as classpath in the standalone Spring project. So, I have moved the configuration files to src/resources folder to import them without any hassle.

log4j debug not logging to any of the configured appenders

Logger.getRootLogger().getAppender("CONSOLE") returning null in a class but it can log to console. getAllAppenders() also returning NullEnumeration in this class.
In another class(MyClass) Logging level:DEBUG | Additivity:true | Parent:root | getAllAppenders():NullEnumeration
but log.debug printing nothing to console.
log4j configuration for this class is
<logger name="com.xxx.service.impl.MyClass">
<level value="TRACE"/>
</logger>
tried to debug why is debug message not getting printed in many ways but in vain.
How can I check the log4j configuration for this class in depth and what could be the problem, please help me.
At first, u should set additivity parameter as false to be sure that each class is writing its logs to one log4j file:
log4j.additivity.${APPENDARNAME}=false
And make sure that only one log4j tech is loaded with your app(I prefer only log4j for basic staff)
Be sure that , no log4j.xml or log4j.properties files are under any of your classpath folders but the one that u want to be loaded. if that is a possiibility, force application to use yours. For spring:
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
<property name="staticMethod">
<value>org.springframework.util.Log4jConfigurer.initLogging</value>
</property>
<property name="arguments">
<list>
<value>file:${log4jFile}</value>
</list>
</property>
</bean>
Adding below listener in web.xml solved my problem.It configured my desired log4j.xml and log4j is logging fine.
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<!-- applies log4j configuration -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

can i know the log directory path in applicationcontext.xml?

I'm writing a java Application for Tomcat 7.
I have a bean configuration for a class that creates a log file and appends information to it.
now the question is how can I know the tomcat log directory path in bean configuration.
for now I have the following bean:
<bean id="foo_logger" class="com.bar.LoggerBean">
<!-- <property name="logPath" value="/path/DWHEventGenerator.log"/> -->
<property name="logPath" value="/var/lib/tomcat7/logs/mylog.log"/>
<property name="logLevel" value="ALL"/> <!-- ALL, FINE/R/ST, INFO, SEVERE, WARNING, OFF -->
</bean>
what i'd like to do is instead of specify /var/log/tomcat7/log, is to specify some variable that will indicate the actual path of the logs directory of tomcat. is that possible ?
thank you.
The simplest approach would be to use catalina.base from the system properties in the logPath property value
<property name="logPath" value="${catalina.base}/logs/mylog.log"/>
This property will be set by Tomcat's launch script (catalina.sh/catalina.bat) so will be available for use when Spring loads the application context file.

Upload Files In Spring misses jar file

I tried to have an upload option for my spring web app, and I add following resolver
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="500000"></property>
</bean>
I also included two jar files in my WEB-INF/lib folder: commons-fileupload-1.3.jar and commons-io-2.4.jar. But when I run it, it still reports the error:
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2398)
java.lang.Class.getDeclaredConstructors(Class.java:1838)
WHat am I still missing? I checked that the FileItemFactory is indeed in my Web App Library.
Thanks
It seems I put some of the jar files to my tomcat common shared lib folder, and some to my WEB-INF/lib folder. I moved everything to my webapp lib folder, and now it works fine.

Spring properties priority

I've got standart maven folder structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
My appicationContext contains following:
<!-- load properties files -->
<context:property-placeholder location="classpath*:*.properties"/>
I have defined 2 hibernate.properties files - one for src/main/resources and one for src/ test/resources. I have expected that when I will run tests my test hibernate.properties will override production hibernate.properties. Instead of that both files are loaded and production version is used:
Loading properties file from file [D:\projects\video_crawler_v3\out\test\core\hibernate.properties]
Loading properties file from file [D:\projects\video_crawler_v3\out\production\core\hibernate.properties]
How can I correctly setup my properties files? I'm using Intellij IDEA to compile and run tests
One of the options is Spring Profiles http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
Put two "properties" versions in your context.xml, eg:
<beans>
... your beans
<beans profile="prod">
<context:property-placeholder location="classpath:/hibernate.properties" />
</beans>
<beans profile="test">
<context:property-placeholder location="classpath:/test-hibernate.properties" />
</beans>
</beans>
Activate required profile with -Dspring.profiles.active=test.
Note: use www.springframework.org/schema/beans/spring-beans-3.1.xsd
The files in src/main/resources are always added to the classpath, even when running the unit tests. See this: Common strategies when defining Spring beans for different environments

Categories

Resources