1

Web アプリを Spring 3.2 に移行していて、web.xml を使用しない構成を楽しんでいます。残っている部分の 1 つは、以前に web.xml で次のように行った webapp ルート キーの設定です。

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapproot</param-value>
</context-param>

Spring がデフォルトのキーを作成することは知っていますが、私の場合、同じ war の複数のバージョンを実行しており、それぞれでキーを異なる値に設定する必要があります。したがって、最適には、プロパティ ファイルから値を取得し、それをルートキーとして使用したいと考えています。

ここのどこかでこれを行うと思います:

public class WebAppInitializer implements WebApplicationInitializer {
    private static final Logger logger = Logger.getLogger(WebAppInitializer.class);

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Create the root appcontext
       AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
       rootContext.register(AppConfig.class);

       servletContext.addListener(new WebAppRootListener());

    // Manage the lifecycle of the root appcontext
       servletContext.addListener(new ContextLoaderListener(rootContext));
       //servletContext.setInitParameter("defaultHtmlEscape", "true");

    // The main Spring MVC servlet.
       ServletRegistration.Dynamic springapp = servletContext.addServlet(
          "springapp", new DispatcherServlet(rootContext));
            springapp.setLoadOnStartup(1);
       Set<String> mappingConflicts = springapp.addMapping("/");
...etc...

アドバイスを提供できる人に感謝します!

4

1 に答える 1

1

最初の部分は単純でした:

servletContext.setInitParameter("webAppRootKey", getRootKey());

この場合、maven ビルドによって application.properties ファイルに追加されるルートキーを取得するには、

private String getRootKey() {
    Properties prop = new Properties();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();           
    InputStream stream = loader.getResourceAsStream("application.properties");
    String key=null;
    try {
        prop.load(stream);
        key = prop.getProperty("rootKey");
    } catch  (Exception e) {
        throw new RuntimeException("Cannot load webapprootkey", e);
    }
    return key;
}
于 2013-07-16T23:46:11.640 に答える