luca
luca

Reputation: 3328

Best way to read properties file in Spring

I'm using Spring and I need to use some properties file to retrieve information in several classes. What is the best way avoiding xml code but only with annotation? For example I tried with this code:

@PropertySource(value = { "classpath:application.properties" })
public class FleetFolderName {

    @Autowired
    private static Environment env;

    private static final String PROPERTY_NAME_FILESYSTEM_BASEPATH = "filesystem.basepath";

    public static String createFleetName(Fleet fleet){
        String path=env.getRequiredProperty(PROPERTY_NAME_FILESYSTEM_BASEPATH) + fleet.getApplication() + " " +  
                fleet.getCubic() + " " + fleet.getPower() + " " + fleet.getTransmission() + " " + fleet.getEuroClass();
        return path;

but env variable is null so I receive exception.This is the same approach of my configuration class but there works fine

@EnableWebMvc
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@ComponentScan({ "com.*" })
@EnableTransactionManagement
@Import({ SpringMvcInitializer.class })
@EnableJpaRepositories("com.repository")
public class AppConfig extends WebMvcConfigurerAdapter{
    @Autowired
    private Environment env;

UPDATE With @Imran code:

public class FleetFolderName {

    @Value("filesystem.basepath")
    private static String PROPERTY_NAME_FILESYSTEM_BASEPATH;

    public static String createFleetName(Fleet fleet){
        String path= PROPERTY_NAME_FILESYSTEM_BASEPATH + fleet.getApplication() + " " +  
                fleet.getCubic() + " " + fleet.getPower() + " " + fleet.getTransmission() + " " + fleet.getEuroClass();
        return path;

configuration class:

@EnableWebMvc
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@ComponentScan({ "com.*" })
@EnableTransactionManagement
@Import({ SpringMvcInitializer.class })
@EnableJpaRepositories("com.repository")
public class AppConfig extends WebMvcConfigurerAdapter{
    @Autowired
    private Environment env;

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
    private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";

    //Reead properties file so can access to its properties through @Value
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        List<Resource> resources = new LinkedList<Resource>();
        resources.add(new ClassPathResource("application.properties"));
        //resources.add(new ClassPathResource("config2.properties"));
        configurer.setLocations(resources.toArray(new Resource[0]));
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer; 
    }

Project structure:

enter image description here

Upvotes: 3

Views: 8685

Answers (2)

Dapper Dan
Dapper Dan

Reputation: 1072

If you are using SpringBoot, using annotation could be the best to access properties file.

@Value("${proprtyName}")
private String propertyvalue;

Upvotes: 0

Imrank
Imrank

Reputation: 1019

Define a bean of PropertySourcePlaceholderConfigurer class in you WebMvcConfigurerAdapter to load properties files.

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    List<Resource> resources = new LinkedList<Resource>();
    resources.add(new ClassPathResource("config.properties"));
    resources.add(new ClassPathResource("config2.properties"));
    configurer.setLocations(resources.toArray(new Resource[0]));
    configurer.setIgnoreUnresolvablePlaceholders(true);
    return configurer;

}

After that you can access all the propperties of config.properties file through annotation

@Value("${proprtyName}")

If you have few more properties files you can annotate your config class to include those properties files something as given below.

@PropertySource(value="config2.properties")
@Configuration
public class ConfigHandler{
}

Project structure:

enter image description here

Upvotes: 2

Related Questions