将Spring Web应用程序(web.xml)迁移到Spring Boot Executable Jar

时间:2017-09-13 13:43:16

标签: spring-boot

好的,我已经做了很多谷歌搜索,我似乎无法找到一个明确的答案。让我们尽可能简单。我有一个web.xml文件

<listener>
  <listener-class>A</listener-class>
</listener>

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:springcontexts/*.xml</param-value>
</context-param>

<context-param>
  <param-name>contextClass</param-name>
  <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<servlet>
  <servlet-name>spring-ws</servlet-name>
  <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:wsspringcontexts/*.xml</param-value>
  </init-param>
</servlet>

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring_mvc_contexts/*.xml</param-value>
  </init-param>
</servlet>

我想我知道如何将其迁移到Spring Boot ......

@SpringBootApplication(exclude = DispatcherServletAutoConfiguration.class)
@ImportResource("classpath*:springcontexts/*.xml")
public class Application
{
  public static void main(String[] args)
  {
    SpringApplication.run(Application.class, args);
  }
}

子包装中的某个地方......

@Configuration
@EnableWebMvc
public class SpringMVCConfiguration
{
  @Bean
  public ServletRegistrationBean mvc()
  {
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.setConfigLocation("classpath*:spring_mvc_contexts/*.xml");
    // the dispatcher servlet should automatically add the root context
    // as a parent to the dispatcher servlet's applicationContext
    DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/spring/*");
    servletRegistrationBean.setName("DispatcherServlet");
    return servletRegistrationBean;
  }
}

...我们再次为其他Servlet执行上述操作

我的第一个问题是如何添加听众&#34; A&#34;到Spring Boot并确保它在刷新根应用程序之前运行?一些配置的bean需要设置一些静态字段(遗留代码),并且此设置在监听器&#34; A&#34;中完成。当使用上面的web.xml

作为标准战争部署时,这可以正常工作

此外,上面的Spring Boot设置看起来是否正确?

1 个答案:

答案 0 :(得分:0)

为什么不把你的遗留初始化放在bean的postConstruct方法中呢?

如果没有,你可以添加一个实现

的监听器

ApplicationListener<ContextRefreshedEvent>

并覆盖

public void onApplicationEvent(final ContextRefreshedEvent event)

你的Spring Boot设置看起来不错吗?很难说,虽然我让Spring Boot为你自动配置调度程序servlet之类的东西,并尽可能摆脱任何XML配置。