在Spring中读取属性文件的最佳方法

时间:2015-11-10 08:23:31

标签: java spring spring-mvc properties environment-variables

我正在使用Spring,我需要使用一些属性文件来检索多个类中的信息。 避免使用xml代码但仅使用注释的最佳方法是什么? 例如,我尝试使用此代码:

@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;

但是env变量为null所以我收到异常。这是我的配置类的相同方法但是工作正常

@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;

更新使用@Imran代码:

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;

配置类:

@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; 
    }

项目结构:

enter image description here

2 个答案:

答案 0 :(得分:1)

在WebMvcConfigurerAdapter中定义PropertySourcePlaceholderConfigurer类的bean以加载属性文件。

@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;

}

之后,您可以通过注释

访问config.properties文件的所有属性
@Value("${proprtyName}")

如果你有更多的属性文件,你可以注释你的配置类,以包含下面给出的那些属性文件。

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

项目结构:

PHP Docs

答案 1 :(得分:0)

如果您使用的是SpringBoot,使用注释可能是访问属性文件的最佳选择。

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