首页 文章

将Spring Web Application(web.xml)迁移到Spring Boot Executable Jar

提问于
浏览
0

好吧我已经做了很多谷歌搜索,我似乎无法找到一个明确的答案 . 让我们尽可能简单 . 我有一个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执行上述操作

我的第一个问题是如何将侦听器“A”添加到Spring Boot并确保它在刷新根应用程序之前运行?一些配置的bean需要设置一些静态字段(遗留代码),并且此设置在侦听器“A”中完成 . 使用上面的web.xml作为标准战争部署时,这可以正常工作

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

1 回答

  • 0

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

    如果失败,你可以添加一个实现的监听器

    ApplicationListener<ContextRefreshedEvent>

    并覆盖

    public void onApplicationEvent(final ContextRefreshedEvent event)

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

相关问题