【问题标题】:Best way to read properties file in Spring在 Spring 中读取属性文件的最佳方法
【发布时间】:2016-02-11 01:41:47
【问题描述】:

我正在使用 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 变量为空,所以我收到异常。这与我的配置类的方法相同,但工作正常

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

项目结构:

【问题讨论】:

  • @Value 有什么问题
  • 同WebMvcConfigurerAdapter

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    在你的 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{
    }
    

    项目结构:

    【讨论】:

    • 如果我有多个属性文件,如何识别不同的文件?
    • 您可以以相同的方式添加它,也可以使用@PropertySource 注释来包含这些属性文件。查看我的更新答案
    • 我试过了,但 PROPERTY_NAME_FILESYSTEM_BASEPATH 为空
    • 尝试使用@Component 注释 FleetFolderName 类或在 WebMvcConfigurerAdapter 中将其声明为 bean 类
    • 我尝试使用@Component 但没有用。我也试过@Value("${filesystem.basepath}")
    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多