Last updated on February 28th, 2024
If some values are changing in the future then these values are defined in the source better to define the properties file. Because it will be changed at any time when required to change there is no effect on the project. We can define database configuration, upload folder path, etc. in the properties file.
In this example, we will inject the values of a team name and the match from the properties file. Because in the future team name will name and also chance to change the match. That’s why we will inject the team and match from the properties file into the project. Injecting values from properties file is helpful when we will work on a real-time project and that project needs a database connection. Because it may change a database in the future for a project like MySql to PostgreSQL.So the database configuration of the project is defined in the properties file better choice for future enhancement.
Sample of a properties file:
key_name=key_value
key_name=key_value
--------------------------------
---------------------------------
POJO Class:
class Test {
----------------
-----------------
}
Sample configuration of the properties file into the XML configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- load the properties file -->
<context:property-placeholder location="classpath:properties_file_name"/>
<!--Define your beans here-->
<bean id="bean_id_name" class="package_name_bean_class">
<!-- inject values from properties file -->
<property name="property_name" value="${key_name}"/>
<property name="property_name? value="${key_name}"/>
</bean>
</beans>
In this example of the project, we use a sport.properties file to define some key names and their value i.e., sport.team=Kings XI Punjab and sport.match = T20. We will create a POJO class i.e., Cricket.java and XML configuration file i.e., applicationContext.xml file. In this XML configuration file, we will configure a Cricket.java POJO class as a Spring Bean class and inject values from the application.properties file. In this XML configuration file, we will define key names which are defined in the application.properties file. We will create the main class i.e., ClientApp.java which contains code for active Spring Container and load the applicationContex.xml file. Then we will get a Spring Bean class and access its method to values from the properties file print output on the console.
Development Process:
1. Keep eclipse IDE ready
2. Project Structure
3. Create a Properties File
4. Create a Bean Class
5. Configure your Spring Beans and load properties in the XML file
6. Add the jar file to the Build Path of the Project for the example of the injecting values from the properties file in XML
7. Run the App
1. Keep eclipse IDE ready
2. Project Structure
3. Create a Properties File
sport.properties:
sport.team=Kings XI Punjab
sport.match=T20
4. Create a Bean Class
Cricket.java(Pojo class):
package com.poc.bean;
public class Cricket {
private String teamName;
private String match;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getMatch() {
return match;
}
public void setMatch(String match) {
this.match = match;
}
public String getMatchDetail() {
return "Match Detail: " + teamName + "-" + match;
}
}
5. Configure your Spring Beans and load properties in the XML file
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- load the properties file:sport.properties -->
<context:property-placeholder location="classpath:sport.properties"/>
<!--Define your beans here-->
<bean id="cricket" class="com.poc.bean.Cricket">
<!-- inject values from properties file -->
<property name="teamName" value="${sport.team}"/>
<property name="match" value="${sport.match}"/>
</bean>
</beans>
→ ${-} is used for getting values from the properties file.
→ Spring IoC container injects value from a properties file to Spring bean class’s setter methods.
6. Add the jar file to the Build Path of the Project for the example of the injecting values from the properties file in XML
Right Click on Project(Injecting_Values_Properties_File) -> Build Path -> Java Build Path -> libraries ->Add External jars
Commons-logging-<version>.jar
spring-beans-<version>.jar
spring-context-<version>.jar
spring-context-support-<version>.jar
spring-core-<version>.jar
spring-expression-<version>.jar
ClientApp.java:
package com.poc.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.poc.bean.Cricket;
public class ClientApp {
public static void main(String[] args) {
/ load the spring Configuration file
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
//retrieve bean from spring container
Cricket cricket = context.getBean("cricket", Cricket.class);
// call the method on bean
System.out.println(cricket.getMatchDetail());
context.close();
}
}
7. Run the App
Output:
Match Detail: Kings XI Punjab-T20
Conclusion:
This example is explained How to inject values from the Properties file? How to load the Properties file in the config file?