0

我最喜欢的 Spring 特性之一是如何处理从文件加载的属性。你只需要像下面这样设置一个bean

<bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="/WEB-INF/app.properties" />
</bean> 

现在,您可以使用 xml(见下文)或注解将从 app.properties 加载的属性值注入到您的 bean 中。

<bean class="com.example.PersonDaoImpl">
    <property name="maxResults" value="${results.max}"/>
</bean>

results.max 是属性之一。

我非常喜欢此功能,因为它使我能够创建非常灵活的应用程序,我只需更改一个属性即可打开/关闭某些功能 - 无需重新部署应用程序。

现在我正在使用 JBoss Seam,我一直在努力寻找一种方法来使用这个框架做类似的事情。

有谁知道该怎么做?如果没有,是否有人知道我如何使用 Seam 以一种很好的方式处理属性(我已经看到了一些方法 - 但它们都不够好)。

谢谢。

4

1 回答 1

0

如果没有适当的方式使用您的软件堆栈(真的没有依赖注入!?)。我会说:使用 Google Guice ( https://code.google.com/p/google-guice/ , https://code.google.com/p/google-guice/wiki/Motivation?tm=6 ) !

Guice 的坏处:您可能需要大量阅读才能了解它是如何工作的,以及您可以用它做什么。但是在它运行之后,你只需将你的Properties对象注入你需要的地方:

class YourClass {
  @Inject Properties myProperties;

  @Inject
  public YourClass() { ... }

  public void someMethod() {
    use the property
  }
}

或者如果你需要构造函数中的属性,你也可以这样做:

class YourClass {
  final Properties myProperties;

  @Inject
  public YourClass(Properties myProperties) { 
    this.myProperties = myProperties;
  }

  public void someMethod() {
    use the property
  }
}

使用 Guice 可能会强制您重构整个应用程序。

但是如果你已经有一些 DI 框架,你应该简单地使用它:)

于 2013-03-01T02:19:11.903 回答