2017-09-13 136 views
0

好吧我已经完成了大量的Google搜索,而且我似乎无法找到明确的答案。让我们尽可能地简单。我有一个web.xml文件将Spring Web应用程序(web.xml)迁移到Spring Boot可执行文件夹

<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> 

我想我知道如何将这种迁移到春季启动...

@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设置看起来是否正确?

回答

0

为什么不把你的传统初始化放在bean的postConstruct方法中?

如果做不到这一点,你可以添加实现

ApplicationListener<ContextRefreshedEvent>

,并覆盖

public void onApplicationEvent(final ContextRefreshedEvent event)

贵春启动的设置看行监听器?很难说,尽管我会让Spring Boot自动为你配置调度程序servlet之类的东西,并尽可能地除去任何XML配置。

+0

我需要重现上面web.xml中给出的逻辑。运行遗留代码的侦听器需要ServletContext,并在Spring侦听器运行(它引导Spring)之前运行。我必须能够重现上面在Spring Boot中给出的web.xml? – paul

相关问题