3

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

2
  • Anything wrong with @Value Commented Nov 10, 2015 at 8:24
  • it is the same of WebMvcConfigurerAdapter Commented Nov 10, 2015 at 9:58

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

and if i have several properties files how can I recognize the different files?
you can add it in same way or you can use @PropertySource annotation to include those properties files. see my updated answer
I have tried but PROPERTY_NAME_FILESYSTEM_BASEPATH is null
Try annotating FleetFolderName class with @Component or declare it as bean class in WebMvcConfigurerAdapter
I tried with @Component and didn't work. I have tried also @Value("${filesystem.basepath}")
|
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.