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:

@Value