1

I am using Java+Spring+spring XML configuration in my project.

I would like to read one property value from the property file and set java value in spring configuration using input String value.

MyClass.class

private String tableDetails;
private String logpath;

myTest.properties

log_path=C:\test\app
table1_details=table1Name|table1Key|query1
table2_details=table2Name|table2Key|query2
table3_details=table3Name|table3Key|query3

Spring_config.xml

<bean id="myClass" class="com.test.MyClass">
        <property name="logpath" ref="${log_path}"/>
<property name="tableName" value="#{systemProperties['checker.table']}"/>        
        <property name="tabledetails" value="${#{systemProperties['checker.table']}}"/>

suppose checker.table = table1_details then

<!--working-->
<property name="tableDetails" value="${table1_details}"/> 
<!--not working-->
<property name="tableDetails" value="${#{systemProperties['checker.table']}}"/> 

So the requirement is that I have property name in systemProperties['checker.table'] which I am not able to use in value field to read the property details of table1_details and set the tableDetails in MyClass?

Tokendra Kumar Sahu
  • 3,524
  • 11
  • 28
  • 29

2 Answers2

1

In your java/pojo class to take value from properties file write -

@value("${table1_details}")
String tableDetails;

@value("${log_path}")
String logpath;

You also have to mention your properties file in xml -

<context:place-holder location="classpath*:myTest.properties">

And to read value of POJO in xml file call get method like -

<bean id="abc" class = "qwe.ert.MyClass"/> 
<bean id="xyz" class= "qwe.ert.NewClass">
    <property name="tableDetails" value="#{abc.getTableDetails()}">
    <property name="log" value="#{abc.getLogPath()}">
</bean>
Khoyendra Pande
  • 1,627
  • 5
  • 25
  • 42
0

Annotate your class with

@PropertySource("sourceOfProperty")

inject value to field

@Value(${property})

You can also access property from Environment

env.getProperty("property")
fg78nc
  • 4,774
  • 3
  • 19
  • 32