Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Integrating External Property Files in Spring Applications

Notes May 7 3

The Spring Framework provides mechanisms to load properties from external .properties files, enabling dynamic configuration injection into managed beans. This is particularly useful for externalizing environment-specific settings like database configurations.

Consider a scenario where a Druid connection pool needs to be configured using an external file.

Step 1: Add Required Depandencies Include the Druid and MySQL driver dependencies in your project's pom.xml.

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.8</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>

Step 2: Create the Property File Place a file named database.properties in the src/main/resources directory with the following content:

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/app_database?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
db.user=app_user
db.pass=secure_password

Step 3: Configure Spring XML Context Update the Spring application context XML file to include the context namespace and load the property file. The properties are then used to configure the Druid DataSource bean.

<?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 external property file -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- Define the DataSource bean with property injection -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.user}"/>
        <property name="password" value="${db.pass}"/>
    </bean>

</beans>

Step 4: Verify Configuration The configured DataSource bean can now be retrieved from the Spring ApplicationContext and used within the application, with its properties sourced from the external file.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.