2015-08-19 83 views
0

我想通過基於註釋的配置來替換web.xml中的spring上下文的配置。web.xml配置的Spring註釋配置替換

什麼是最好的方法來保持配置的方式是相同的,但沒有更多的web.xml中的單獨的XML配置文件。

當前每個列出的上下文都是相互加載的,以便具有一些「引導階段」。 然後有上下文文件填充應用程序上下文(由每個servlet共享)和servlet特定的xml配置文件。

起始點應該是web.xml中的一個單引用,用初始的具有@Configuration註解的RootContext java類。但是,如何繼續,保持單獨的上下文,就像現在一樣?

這裏當前的web.xml片段:

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/root-context.xml 
      /WEB-INF/spring/2nd-context.xml 
      /WEB-INF/spring/3rd-context.xml 
      /WEB-INF/spring/4th-context.xml 
     </param-value> 
    </context-param> 

<servlet> 
     <servlet-name>springWebapp</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/spring/servlet/servlet-context1.xml 
       /WEB-INF/spring/servlet/servlet-context2.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

我的幫助非常感激。

+0

雖然不是一個直接的答案,但[這](http://stackoverflow.com/questions/31983613/implementing-applicationcontextaware-applicationcontext-is-null)後有相當全面的基於Java的配置,可以被緩衝。 –

回答

2

更換

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/root-context.xml 
      /WEB-INF/spring/2nd-context.xml 
      /WEB-INF/spring/3rd-context.xml 
      /WEB-INF/spring/4th-context.xml 
     </param-value> 
    </context-param> 

通過

@Configuration 
@ImportResource({ 
    "classpath:WEB-INF/spring/root-context.xml", 
    "classpath:WEB-INF/spring/2nd-context.xml", 
    "classpath:WEB-INF/spring/3rd-context.xml", 
    "classpath:WEB-INF/spring/4th-context.xml" 
}) 
public class AppConfig { 
} 

也可以在AppConfig中分離CONFIGS成單獨的類和importClass。

@Configuration 
@ImportResource({ 
    "classpath:WEB-INF/spring/2nd-context.xml", 
    "classpath:WEB-INF/spring/3rd-context.xml", 
    "classpath:WEB-INF/spring/4th-context.xml" 

}) 
public class AppSecondConfig { 
} 

@Configuration 
@ImportResource({ 
    "classpath:WEB-INF/spring/root-context.xml" 
}) 
@Import(AppSecondConfig.class) 
public class AppConfig { 
} 

編輯:對於以多進口 - @Import({AppSecondConfig.class,AppThirdConfig.class})

更換

<servlet> 
     <servlet-name>springWebapp</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/spring/servlet/servlet-context1.xml 
       /WEB-INF/spring/servlet/servlet-context2.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

通過

@Configuration 
@ImportResource({ 
    "classpath:WEB-INF/spring/servlet/servlet-context1.xml", 
    "classpath:WEB-INF/spring/servlet/servlet-context2.xml" 
}) 
public class ServletConfig { 
} 

並告訴你的web.xml現在可以像這樣引用配置類

<context-param> 
     <param-name>contextClass</param-name> 
     <param-value> 
     org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
     </param-value> 
    </context-param> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>com...config.AppConfig</param-value> 
    </context-param> 

<servlet> 
     <servlet-name>spring-mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextClass</param-name> 
      <param-value> 
       org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
      </param-value> 
     </init-param> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>com....config.ServletConfig</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
+0

你是否認爲上下文類的順序將保持這種方式?支持RootContext將首先完成加載,然後AppSecondConfig,然後AppThirdConfig。 –

+0

如果你想要多次導入,你可以這樣做@Import({AppSecondConfig.class,AppThirdConfig.class}) –

+0

對於多個配置類[spring doc](http://docs.spring.io/spring-javaconfig/ docs/1.0.0.M4/reference/html/ch04s03.html)[spring doc2](http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html #豆-JAVA) –