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