【问题标题】:When using Dependency Injection, how do I ignore missing properties?使用依赖注入时,如何忽略缺失的属性?
【发布时间】:2012-10-18 16:40:23
【问题描述】:

我正在尝试使我的应用程序向后兼容至少一个以前的版本。我遇到的最大问题是旧属性文件中不存在的预期属性。

例如,假设旧的属性文件如下所示:

prop.old=some text

新的属性文件如下所示:

prop.old=some text
prop.new=some new text

而新的 app-context.xml 的相关部分如下所示:

<beans:bean id="myClass" class="com.mycompany.MyClass">
    <beans:property name="oldThing" value="${prop.old}" />
    <beans:property name="newThing" value="${prop.new}" />
</beans:bean>

显然,这会在运行时崩溃。有没有办法检查属性是否存在,如果不存在,则使用空字符串代替?

【问题讨论】:

  • 可能是的。其他 DI 容器提供了这个功能,但是我对 Spring 的了解还不够多。

标签: spring dependency-injection


【解决方案1】:

从 3.0 开始,您可以在 PropertyPlaceholderConfigurer 中使用 ignore-unresolvable=true

<context:property-placeholder ignore-unresolvable="true" />

【讨论】:

    【解决方案2】:

    这很棘手,因为如果您尝试忽略缺失的属性,您将无法判断缺失的属性是强制性的还是可选的。所以我更喜欢编程方式。

    首先,有一个基类来访问任何属性文件:

    public abstract class ClasspathProperties {
    
        private final Environment environment;
    
        public ClasspathProperties(final Environment environment, final String propertiesLocation) {
            try {
                final PropertySource<?> propertySource = new ResourcePropertySource(propertiesLocation);
                verifyProperties(propertySource);
                ((ConfigurableEnvironment) environment).getPropertySources().addFirst(propertySource);
                this.environment = environment;
            } catch (final IOException e) {
                throw new IllegalStateException("reading properties from location " + propertiesLocation + " failed", e);
            }
        }
    
        public Environment getEnvironment() {
            return environment;
        }
    
        protected abstract void verifyProperties(PropertySource<?> propertySource);
    
        protected void verifyPropertyExistence(final PropertySource<?> propertySource, final String propertyName,
                final String propertyDescription) {
            final Object propertyValue = propertySource.getProperty(propertyName);
            if (propertyValue == null || "".equals(propertyValue)) {
                throw new IllegalStateException(propertyDescription + " is not set");
            }
        }
    
    }
    

    然后您可以在设置属性之前读取指定的属性文件并进行验证:

    public class ClasspathDatabaseProperties extends ClasspathProperties implements DatabaseProperties {
    
        public ClasspathDatabaseProperties(final Environment environment) {
            this(environment, "classpath:/config/db-config.properties");
        }
    
        public ClasspathDatabaseProperties(final Environment environment, final String propertiesLocation) {
            super(environment, propertiesLocation);
        }
    
        @Override
        protected void verifyProperties(final PropertySource<?> propertySource) {
            verifyPropertyExistence(propertySource, "mysql.host", "MySQL DB host");
            verifyPropertyExistence(propertySource, "mysql.port", "MySQL DB port");
            verifyPropertyExistence(propertySource, "mysql.database", "MySQL DB DB name");
            verifyPropertyExistence(propertySource, "mysql.username", "MySQL DB username");
            verifyPropertyExistence(propertySource, "mysql.password", "MySQL DB password");
        }
    
        @Override
        public String getHost() {
            return getEnvironment().getProperty("mysql.host");
        }
    
        @Override
        public int getPortNumber() {
            return getEnvironment().getProperty("mysql.port", Integer.class);
        }
    
        @Override
        public String getDatabase() {
            return getEnvironment().getProperty("mysql.database");
        }
    
        @Override
        public String getUsername() {
            return getEnvironment().getProperty("mysql.username");
        }
    
        @Override
        public String getPassword() {
            return getEnvironment().getProperty("mysql.password");
        }
    
    }
    

    如果无法解析密钥,getProperty(String key) 将返回 null。

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 1970-01-01
      • 2012-05-03
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多