In my IntelliJ project, under the project root, there is a config folder.
This is where various .properties files are located.
Within the application, these are made available to Spring in the standard way, using a propertiesConfigurer.xml file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config/${name}.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
The reason they are accessed using 'file', is that eventually they may be external to the deployed jar.
This works, both when running the application, and when running unit tests.
I also wish to access files which are only for use when running tests.
I have placed these under:
test
--- resources
-------META-INF
----------spring
-------------config
I have added this line to the list of locations within the same propertiesConfigurer.xml:
<value>classpath*:META-INF/spring/config/${name}.properties</value>
This is not working. No file or resource exception is being thrown, but none of the properties defined in the test file are being resolved within the application.
Within the IntelliJ Project Settings->Modules, src\test\resources is listed as a context root.
Also, on the file system, under:
...\target\test-classes\META-INF\spring\config
the test property files exist.
What is the correct way to access them?
Thanks
Related
Is it possible to create a data source by reading the values from an external file which is not bundled with WAR in spring application.
You can use the #PropertySource annotation to load your db properties and you can load the properties from file location like below:-
#PropertySource("file:${app.home}/db.properties")
reference link here:-
https://www.mkyong.com/spring/spring-propertysources-example/
For XML based configuration sample code could be like below:-
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${app.home}/db.properties</value>
</list>
</property>
</bean>
you can setup your datasource in any properties file, and then you have to provide the classpath for that file in your catalina.sh where you are running your war.
don't forget to load that property file in in your application.
I have got a project which works perfectly fine when run via java -jar command but if I run it via junit it fails not able to load property files which are loaded using spring context.Below is the directory structure
/src
/main
/java
/resources
/config
/export
export.properties
context.xml
startJob.bat
test
java
execution
Test1
context.xml contains below:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config/export/export.properties</value>
</list>
</property>
My test class is as below:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath:context.xml"})
public class Test1 {
#Autowired
Export export;
#org.junit.Test
public void test2() {
export.report();
when I run the above junit test case,
I get below error:
Caused by: java.io.FileNotFoundException: config\export\export.properties (The system cannot find the path specified).
I have even tried coping over all the resources from src/main/resources to src/test/resources but it still fails with the same error.
I believe it is something which everyone would be using and must be pretty straighforward...
EDIT 1
i created a separate test-context.xml and placed it in the sources and modified it to load files using classpath:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/export/export.properties</value>
</list>
</property>.
after this spring is able to load the properties but on further processing I have below code in logger: DOMConfigurator.configure("config/export/export_log.xml") which fails with "java.io.FileNotFoundException: H:\workspaces\export\job\config/export/export_log.xml".how does it pick up this path ?
I was able to resolve the issue by configuring log files as below:
DOMConfigurator.configure(JobLogger.class.getClassLoader().getResource("config/export/export_log.xml"))
<bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="fragmentRootElementName" value="SomeElement" />
<property name="unmarshaller" ref="jaxb2unmarshaller" />
</bean>
<bean id="jaxb2unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>org.MappedClass</value>
</list>
</property>
</bean>
I started with multi step job and have issue when packaged as the job module in the spring-xd, I stripped all the other beans from the configuration and finally figured the issue was coming due to the above two beans.
The spring-oxm jar is present in the custom modules lib. I am using spring-xd 1.0.0.RC1.
I don't want to put the jars in to the server lib, all the required libraries should be part of the custom module lib directory.
How to get it working?
If I place the spring-oxm jar in the spring-xd/lib directory the error goes but then I get the other errors too.
I guess the real reason for the NoClassDefError could be the missing dependencies and the error does not indicate the information about the missing dependent class.
Any inputs would be appreciated?
The information provided is not sufficient to conclude anything. However, you could check the second last line of the code block provided above. The closing <property> tag is missing or it's a TYPO while writing the code here.
The module's dependent jars should go in the module's lib/ directory if they are not on the server class path already. Spring XD 1.1 has improved support for module packaging but in 1.0 you need to install the jars manually. Also, I would suggest upgrading to 1.1.0.M2 or 1.0.3.RELEASE at least.
i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/> -->
<property name="locations" value ="config/springcontext.properties" />
The outcommented one is working and the other one i dont get to work :/ Can someone help?
Edit:
Thx 4 comments so far.
Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?
You can try something like this:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/servlet.properties" />
And define ext.properties.dir property in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:
-Dext.properties.dir=file:/usr/local/etc/
BTW, very useful blog post.
You can use file prefix to load the external application context file some thing like this
<context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
<context:property-placeholder location="classpath*:spring/*.properties" />
If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above
<property name="locations" value ="config/springcontext.properties" />
this will be pointing to web-inf/classes/config/springcontext.properties
This blog can help you. The trick is to use SpEL (spring expression language) to read the system properties like user.home, to read user home directory using SpEL you could use #{ systemProperties['user.home']} expression inside your bean elements. For example to access your properties file stored in your home directory you could use the following in your PropertyPlaceholderConfigurer, it worked for me.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value>
</property>
</bean>
This question is kind of old, but wanted to share something which worked for me. Hope it will be useful for people who are searching for some information accessing properties in an external location.
This is what has worked for me.
Property file contents:
PROVIDER_URL=t3://localhost:8003,localhost:8004
applicationContext.xml file contents: (Spring 3.2.3)
Note: ${user.home} is a system property from OS.
<context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/>
<bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">${PROVIDER_URL}</prop>
</props>
</property>
</bean>
${PROVIDER_URL} got replaced with the value in the properties the file
One way to do it is to add your external config folder to the classpath of the java process. That's how I've often done it in the past.
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />
This works for me.
Local development machine path is C:\apps\tomcat\ath\ath_conf and in server /apps/tomcat/ath/ath_conf
Both works for me
I want to pass the WSDL url for an internal web service into my Spring beans.xml dynamically, using a PropertyPlaceHolderConfigurer.
Here's the scenario:
My web application is deployed in WebLogic 10.3.
The WSDL url is contained in a properties file that is located outside my application (directly under the corresponding domain folder, while my application is inside the autodeploy folder). I set the location of this properties file in my domain's setDomainEnv.cmd file like below:
set JAVA_PROPERTIES=%JAVA_PROPERTIES% %CLUSTER_PROPERTIES% -Dproperty.file.path.config=%DOMAIN_HOME%\Service.properties
This is what my Service.properties file contains:
Service.WSDL.PATH=http://localhost:8088/mockServiceSoap?WSDL
My Spring beans.xml configuration:----
<bean id="file.path" class="java.lang.System" factory-method="getProperty">
<constructor-arg index="0"><value>property.file.path.config</value></constructor-arg>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="file.path"/>
</bean>
<bean id="myServiceId" class="com.test.service.ServiceImpl">
<property name="myServiceSoap">
<ref bean="myService"/>
</property>
</bean>
<bean id="myService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.test.service.ServiceSoap"/>
<property name="wsdlDocumentUrl" value="${Service.WSDL.PATH}"/>
</bean>
I enabled DEBUG log specifically for PPC and this is what I saw in my application log:
INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 178 - Loading properties file from URL [file:D:/bea10.3/user_projects/domains/my_domain/Service.properties]
So, it seems that although the Service.properties file is getting loaded by PPC, the ${Service.WSDL.PATH} is NOT getting replaced.
What am I doing wrong here?
Also, how can I find out if PPC tried replacing the value of the placeholder and with what value? I was hoping the log file would contain that info but there was nothing there.
Any help is appreciated.
I've figured out, that PropertyPlaceholderConfigurer needs to be declared first in the application Context file, otherwise there's no guarantee of the load order. It took me a few hours to realize this.
Try moving the "file.path" bean into the PropertyPlaceHolderConfigurer's location property.