Property placeholders with YamlPropertiesFactoryBean and Spring Boot - spring

I am migrating a small "legacy" application to Spring Boot and ran into an issue with property placeholders in combination with a YamlPropertiesFactoryBean. The application uses Spring XML configuration.
I added a Spring Boot "main" to the project like so:
#SpringBootApplication
#ImportResource("classpath:spring-config.xml")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
I have placed an application.yaml into /config. It is being picked up by Spring boot and I can access properties defined in it via property replacement (${some.prop}) in bean declarations in the imported spring-config.xml. E.g.
<bean id="myRouteBuilder" class="org.camelSpringBoot.test.MyRouteBuilder">
<property name="someProp" value="${prop.from.application.yaml}"/>
</bean>
Except when using a org.springframework.beans.factory.config.YamlPropertiesFactoryBean to read an additional, external configuration file: A property for the file location in the constructor arg of a org.springframework.core.io.FileSystemResource does not get resolved:
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources">
<list>
<bean class="org.springframework.core.io.FileSystemResource">
<constructor-arg value="${service.config.loc}" />
</bean>
</list>
</property>
</bean>
yields
java.io.FileNotFoundException: ${service.config.loc}
If I hard-code the location of the external configuration then properties used in the external YAML file are not resolved either.
application.yaml:
security:
keystore:
loc: /security/my.keystore
external config:
services:
a:
ssl:
secret: ${security.keystore.loc}
java.lang.RuntimeException: Cannot open keystore/truststore ${security.keystore.loc}
Note that the error complains about the verbatim property.
The YamlPropertiesFactoryBean instance is declared in spring-config.xml like the other beans that successfully use property placeholders (meaning properties get replaced by their values). Why would it fail for the YamlPropertiesFactoryBean
Is it possible to enable placeholder resolution for the external YAML document?

I do not know if it is working what you are trying to do. The Property resolving takes place after a BeanDefinition is done, and therefore only working with Spring Beans, but not arbitrary files like your yaml file.
Most build tools (gradle, maven) contain a functionality to resolve those placeholder during build time. Might that be a solution for you?

Related

Spring Application data source configuration from external file

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.

Can Spring support multiple messages.properties files in classpath?

There are a JAR A and JAR B, both have messages.properties in classpath /
And there is WAR C, which has dependencies on JAR A and JAR B.
And when I start WAR C I can only get i18n message from JAR A or JAR B.
So, how can Spring support multiple messages.properties files in classpath?
BTW, WAR C is a spring boot project, and spring.messages.basename=messages
Problem solved.
According to this question Does Spring MessageSource Support Multiple Class Path?
have to ditch ResourceBundleMessageSource and write a custom implementation of MessageSource (most likely by subclassing AbstractMessageSource) which uses PathMatchingResourcePatternResolver to locate the various resources and expose them via the MessageSource
I make a copy of ReloadableResourceBundleMessageSource and write the code by this guide and problem solved.
Yes, Spring supports loading multiple Properties. But properties should be unique. If we have same properties in the two Properties Files then the file which will be loaded later will override the previous Properties from previous file.
For example, if the Properties file from JAR A has two properties {USERNAME, PASSWORD} and JAR B Properties file also has the same two properties then you'll get {USERNAME, PASSWORD} from the file which is loading later.
You can use wildcard to import all the Property Files with same name on your classpath.
In Spring you can mention different Properties files in the form of Array as follows:
<context:property-placeholder
location="classpath:war.properties,
classpath*:message.properties"
ignore-unresolvable="true"/>
or
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:war.properties</value>
<value>classpath*:message.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

Failed to load properties from junit spring

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"))

in spring : how to handle name not found exception in application-context.xml?

I am creating a spring project using Oracle and jboss server.
I have one bean in my application-context.xml.
<!-- Datasource for TaskManager -->
<jee:jndi-lookup id="tmTestDataSource"
jndi-name="test_datasource" expected-type="javax.sql.DataSource"/>
<bean id="tmTestJdbcTemplate" name="TmTestJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="tmTestDataSource" />
</bean>
If the 'test_datasource' JNDI (one datasource.xml file in jboss deployment folder, which contains db credentials like url, uid, password) inside the file found deployment is successfull, but if the file is not there then the deployment is failing.
I want to handle this exception so that deployment should not fail.
how I can do this. Can anybody provide me any solution.
Thanks.

Tomcat Context-Params ignored in Spring webapp when using PropertyPlaceholder

I was previously using, the now deprecated, class org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer to load a properties file from the server's filesystem. I had the following bean definied:
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="locations" value="${config}"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="false"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="searchSystemEnvironment" value="false"/>
</bean>
The config is an argument that is passed when starting Tomcat, i.e.
-Dconfig=/path/to/application.properties
For the webapp I also have a context file:
<Context docBase="/path/to/application.war">
<Parameter name="host" value="localhost" override="false"/>
<Parameter name="port" value="8080" override="false"/>
</Context>
If the .properties file, specified by the -Dconfig argument, contains the property that some other bean references then the value from the .properties file is used, otherwise the value from the the context xml file is used.
This allowed me to have a set of default properties deployed with the WAR and if required, I was able to specify a .properties file to override particular values.
Now, I'm updating to use the new property abstractions in Spring 3.1 but I can't seem to figure out what the equivalent approach to this is?
I have the same context file and war deployed in the same way, and I now have the following in the application:
<context:property-placeholder
location="${config}"
system-properties-mode="OVERRIDE"
ignore-resource-not-found="true"
ignore-unresolvable="true"/>
This finds and uses the properties from the properties file, BUT it does not use the values from the context XML file.
How do I get my application to use the context params when using this new property-placeholder?
Thanks.
To summarise the problem is that the Context Parameters from the servlet context file were not being used to resolve placeholders when using the new Property Placeholder namespace introduced in Spring 3.1.
I have figured out a solution, with the following
<context:property-placeholder location="${config}" local-override="true" ignore-resource-not-found="true"/>
I can specify one or more *.properties files on the local filesystem using a JVM arg, eg:
-Dconfig=/path/app.properties
If a placeholder property cannot be resolved after checking the app.properties file then the Servlet Context Parameters are checked.
This allows me to have default values using context params in a the web.xml and where I need to I can override these values by specifying the location of *.properties files using the config JVM arg.
The key to getting it to work this way was to include local-override="true", which is false by default. I'm not fully sure that it makes sense, since the description for that attribute is:
Specifies whether local properties override properties from files. Default
is "false": Properties from files override local defaults.
If the same property key exists in the app.properties and the web.xml the value from the app.properties is used.
Spring uses a default property file unless the user-based property file is defined. If you want to control .properties file, please follow the instructions posted here.
If you want to take advantage of application.properties there are two ways to do it.
<!-- allows for ${} replacement in the spring xml configuration from the
system.properties file on the classpath -->
<util:properties id="appProperties" location="classpath:application.properties"/>
<context:property-placeholder location="classpath:application.properties"/>
util tag lets you to use a Property class to read the properties across your application. For example:
#Autowired
public MyPropertyReader(Properties appProperties) {
String prop1 = appProperties.getProperty("my.address");
String prop2 = appProperties.getProperty("my.version");
}
If you want to use the values within your context file use the context:property-placeholder tag. Then you can use your values as
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/>
where for example jms.primary.server=172.168.10.18:6161 in application.properties.

Resources