2011-07-16 36 views
3

我有几个属性与我的应用程序的配置相关,为了集中配置,我想将其放置到单个文件中。这个应用程序的源代码将被其他人使用和修改,所以我试图通过提供一个配置点来尽可能简化它。Wicket:用于指定全局应用程序属性的模式

我知道如何使用MyComponentName.properties文件来定制组件错误消息,L10N等。但我试图为通常不显示字符串的东西提供配置。一些例子:

  • 电子邮件服务器的主机名
  • 使用什么样的用户认证的
  • 的Facebook应用程序ID

Application.java将从global.properties(或其他)和手工加载这些特性在初始时将适当的配置关闭到我的各个类。我当然可以手动加载文件,但是我想知道Wicket中是否已经有这种支持。

将这些放入web.xml会更好吗?

回答

2

我已经使用了两种方法。

第一种方法,使用的web.xml与检票应用程序初始化参数:

<filter> 
    <filter-name>WicketApp</filter-name> 
    <filter-class> 
     org.apache.wicket.protocol.http.WicketFilter 
    </filter-class> 
    <init-param> 
     <param-name>applicationFactoryClassName</param-name> 
     <param-value> 
     org.apache.wicket.spring.SpringWebApplicationFactory 
     </param-value> 
    </init-param> 
    <init-param> 
     <param-name>param1</param-name> 
     <param-value>xxx.xxx.xxx.xxx</param-value> 
    </init-param> 
    <init-param> 
     <param-name>param2</param-name> 
     <param-value>Hello</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> 
    <filter-name>WicketApp</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

您可以通过访问它们:

MyApplication.get().getInitParameter("param1") 

第二种方法,如果你使用Spring,你可以用你的applicationContext.xml参数化您的beans:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
    <property name="host" value="mail.xxx.com"/> 
    <property name="javaMailProperties"> 
     <props> 
      <prop key="mail.smtp.sendpartial">true</prop> 
     </props> 
    </property> 
</bean> 
相关问题