【问题标题】:Proper way to handle properties files in spring code base configuration在spring代码库配置中处理属性文件的正确方法
【发布时间】:2015-01-17 03:46:48
【问题描述】:

我开始编写应用程序,我想在 java 代码中包含尽可能多的 spring 配置。我遇到的问题是属性文件。看看我到目前为止写了什么:

包含 bean 声明的文件:

@Configuration
@ImportResource("classpath:properties-configuration.xml")
public class ContextConfigutarion {

    @Value("${database.url}")
    private String database_url;

    @Value("${database.user}")
    private String database_user;

    @Value("${database.password}")
    private String database_password;

    @Value("${database.default.shema}")
    private String database_default_shema;

    @Bean
    public BasicDataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
        dataSource.setUrl(database_url);
        dataSource.setUsername(database_user);
        dataSource.setPassword(database_password);
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setHibernateProperties(hibernateProperties);  // <- here I encountered a problem 
        return sessionFactory;
    }
...
}

properties-configuration.xml 是最小必要文件,仅用于指定文件属性位置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:database.properties" />

</beans>   

当我开始配置 sessionFactory 对象时,我发现了一个问题。正如我在 ContextConfiguration 类中所了解的,我必须从我的 database.properties 文件中减去每个属性。如果我的应用程序有很多属性,那么我的配置 java 代码就会冗余增长。没有更好的方法通过 Spring 将属性传输到我的组件而不提取每个组件。

第二个相关问题:有什么好的方法可以在树状结构中保存应用程序属性?因为正如您在上面的示例应用程序属性中看到的那样,包含:数据源属性、休眠属性等,实际上是属性树。如果我的应用程序应该更大并且有更多的组件在树状结构中保留属性会很棒。我想象我有存储在这样的目录中的属性:

/application
    /hibernate
        filename.properties
    /database
        filename.properties
    /someOddComponent
        /someStrangeComponent
            filename.properties
        filename.properties
    filename.properties

因此,如果我要求 application.properties,我将获得应用程序目录(和子目录)中所有 .properties 文件的总和,如果我要求 hibernate.properties,我将获得所有 .properties 文件休眠目录(和子目录)的总和目录)等。也许我在这里夸大了问题,你怎么看?

【问题讨论】:

    标签: java spring hibernate properties


    【解决方案1】:

    首先放弃您的 xml 文件并使用 @PropertySource 注释来加载属性文件并在您的配置类中注册一个 PropertySourcePlaceholderConfigurer

    @Configuration
    @PropertySource("classpath:database.properties")
    public class ContextConfigutarion {
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer ();
        }
    }
    

    如果您有很多属性而不是指定@Value 注释,请改用EnvironmentgetPropertygetRequiredProperty 方法。

    @Configuration
    @PropertySource("classpath:database.properties")
    public class ContextConfigutarion {
    
        @Autowired
        private Environment env;
    
        @Bean
        public BasicDataSource dataSource() {
            BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
            dataSource.setUrl(env.getRequiredProperty("database.url"));
            dataSource.setUsername(env.getRequiredProperty("database.user));
            // Other properties
            return dataSource;
        }
    
    }
    

    您不希望拥有大量配置文件,因为这只会使事情变得复杂且难以维护。将它们限制在一定数量。如果您想要一个类似系统的树,请不要使用属性文件,而是使用 yaml 文件。虽然加载起来有点困难(@PropertySource 不支持),但这允许在单个文件中使用树状配置结构。

    或者更好地让Spring Boot 为您处理复杂性(即支持属性和开箱即用的 yaml 文件)。减少您的配置并为您提供开箱即用的自动配置。

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 2010-09-20
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      相关资源
      最近更新 更多