2017-09-13 134 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

相關問題