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

Best way to read properties file in Spring

我正在使用 Spring,我需要使用一些属性文件来检索多个 class 中的信息。 避免 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 变量为空,所以我收到 exception.This 与我的配置方法相同 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;

更新 使用@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;

配置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; 
    }

项目结构:

在你的 WebMvcConfigurerAdapter 中定义一个 PropertySourcePlaceholderConfigurer class 的 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}")

如果您有更多的属性文件,您可以注释您的配置 class 以包含这些属性文件,如下所示。

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

项目结构:

如果您使用的是 Spring Boot,使用注释可能是访问属性文件的最佳方式。

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