Spring MVC Tutorial

Core Spring MVC

Spring MVC - Annotation

Spring MVC - Form Handling

Spring MVC with JSTL

Spring MVC with REST API

Spring MVC with Database

Spring - How to Load Literal Values From Properties File

In Spring Framework, property files are typically used for configuration, and the values from these files can be loaded into beans. You can load literal values from properties files using the PropertyPlaceholderConfigurer (for traditional XML-based configuration) or using the @PropertySource and @Value annotations (for Java-based configuration).

Here's how to do it for both configurations:

1. XML-Based Configuration:

Step 1: Create a properties file, e.g., application.properties:

app.name=My Spring Application
app.version=1.0.0

Step 2: Configure the PropertyPlaceholderConfigurer in your Spring XML configuration:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
        </list>
    </property>
</bean>

<bean id="myBean" class="com.example.MyBean">
    <property name="appName" value="${app.name}" />
    <property name="appVersion" value="${app.version}" />
</bean>

Step 3: Define the bean that will use the properties:

package com.example;

public class MyBean {
    private String appName;
    private String appVersion;

    // Getters, setters, and other methods...
}

2. Java-Based Configuration:

Step 1: Create a properties file, e.g., application.properties (as shown above).

Step 2: Use the @PropertySource annotation to load the properties file:

@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

Step 3: Inject properties into the bean using the @Value annotation:

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    // Getters, setters, and other methods...
}

Step 4: To resolve ${...} placeholders in @Value annotations, you also need to register a PropertySourcesPlaceholderConfigurer:

@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

With these configurations in place, Spring will load the literal values from the properties file into your beans.

  1. Spring load literal values from properties file:

    In Spring, you can load literal values from a properties file using the @Value annotation. Here's an example:

    @Value("${property.key}")
    private String propertyValue;
    

    In this example, property.key is the key in the properties file, and its corresponding value will be injected into propertyValue.

  2. How to read values from properties file in Spring:

    To read values from a properties file in Spring, you can use the @PropertySource annotation along with Environment:

    @Configuration
    @PropertySource("classpath:application.properties")
    public class AppConfig {
    
        @Autowired
        private Environment environment;
    
        public void someMethod() {
            String propertyValue = environment.getProperty("property.key");
            // Use propertyValue as needed
        }
    }
    
  3. PropertyPlaceholderConfigurer Spring example:

    The PropertyPlaceholderConfigurer is used for placeholder replacement in bean definitions. Here's an example:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:config.properties" />
    </bean>
    

    This configures the PropertyPlaceholderConfigurer to load properties from the config.properties file.

  4. Spring Boot load values from application.properties:

    In a Spring Boot application, values are typically loaded from application.properties or application.yml automatically. Just use the @Value annotation to inject properties:

    @Value("${property.key}")
    private String propertyValue;
    
  5. Injecting properties in Spring beans:

    Properties can be injected into Spring beans using the @Value annotation or through the Environment bean.

    @Value("${property.key}")
    private String propertyValue;
    
  6. Spring property file configuration:

    Spring property file configuration involves using @PropertySource to specify the location of the properties file and @Value to inject values.

    @Configuration
    @PropertySource("classpath:config.properties")
    public class AppConfig {
        //...
    }
    
  7. Loading properties file in Spring context:

    You can load properties files into the Spring context using @PropertySource in a configuration class.

    @Configuration
    @PropertySource("classpath:config.properties")
    public class AppConfig {
        //...
    }
    
  8. Accessing properties file values in Spring Controller:

    In a Spring Controller, you can use the @Value annotation or Environment to access property values.

    @Value("${property.key}")
    private String propertyValue;
    
  9. Using @Value annotation in Spring for properties file:

    The @Value annotation is commonly used to inject values from properties files in Spring.

    @Value("${property.key}")
    private String propertyValue;
    

    This injects the value of the property with key property.key into the propertyValue variable.