Spring + @PropertySource with @Value and Environment

 

In Spring we can configure our application properties in property file and by @PropertySource annotation we can configure externalized configuration file. Those property values can be read by using @Value annotation or by using Environment class.
In below lets see simple example how to use @PropertySource to read property file and to fetch those values using @Value and Environment class. Simple example that we need to serve static resources from different locations to configure in Spring and that too separate location for images and separate for text files etc., Those locations are configured in "myConfig.properties" file.

myConfig.properties

staticresourceloader.imageFileLocation.path:file:C:\\images\\

staticresourceloader.txtFileLocation.path:file:C:\\txtfiles\\



Reading values using @Value annotation

package com.app.staticresourceloader;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@ComponentScan
@EnableAutoConfiguration
@PropertySource("classpath:myConfig.properties")
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter  {

 @Value("${staticresourceloader.imageFileLocation.path}")
    private String staticImageFilePath;
 
 @Value("${staticresourceloader.txtFileLocation.path}")
    private String staticTxtFilePath;
 
 public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        
    }
    
 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        
  System.out.println("IMAGE FILE PATH :::: "+staticImageFilePath);
  System.out.println("TEXT FILE PATH  :::: "+staticTxtFilePath);
  
  registry.addResourceHandler("/api/image/**").addResourceLocations(staticImageFilePath);
        registry.addResourceHandler("/api/txt/**").addResourceLocations(staticTxtFilePath);
        
    }   
}



Reading values using Environment

package com.app.staticresourceloader;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@ComponentScan
@EnableAutoConfiguration
@PropertySource("classpath:myConfig.properties")
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter  {

 @Autowired
 private Environment pathDetails;
 
 public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        
    }
    
 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
  
  String imagePath = pathDetails.getProperty("staticresourceloader.imageFileLocation.path");
  String txtPath = pathDetails.getProperty("staticresourceloader.txtFileLocation.path");
  
  System.out.println("IMAGE FILE PATH :::: "+imagePath);
  System.out.println("TEXT FILE PATH  :::: "+txtPath);
  
  registry.addResourceHandler("/api/image/**").addResourceLocations(imagePath);
        registry.addResourceHandler("/api/txt/**").addResourceLocations(txtPath);
        
    }   
}


OUTPUT:

Spring + @PropertySource with @Value and Environment

No comments:
Write comments