2015-03-31 71 views
0

我有一个关于spring上下文的问题。我的应用程序使用弹簧和弹簧调度器。 在web.xml中,我宣布:Spring上下文加载器

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

我的问题是:

如果我宣布org.springframework.web.context.ContextLoaderListener在web.xml中,调度程序将运行两次,所有的豆类都是重复的,和应用程序启动时间大约160秒。

如果我删除org.springframework.web.context.ContextLoaderListener, 春天抛出异常:No WebApplicationContext found: no ContextLoaderListener registered。并且App启动时间缩短到80秒。

我该如何解决它?谢谢大家!

+1

拆分您的配置。不要让'ContextLoaderListener'和'DispatcherServlet'加载相同的配置文件。如果你这样做,一切都会被加载两次。 – 2015-03-31 08:41:42

+0

你甚至为什么要考虑使用XML配置?它是2015年 - http://www.kubrynski.com/2014/01/understanding-spring-web-initialization.html – 2015-03-31 10:14:00

回答

0

谢谢@ M.Deinum,但我不明白你的想法。 这是我的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.htc.epos.api.bootstrap</param-value> 
    </context-param> 

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

    <servlet> 
    <servlet-name>webapp</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.htc.epos.api.bootstrap.WebAppConfig 
       com.htc.epos.api.bootstrap.AppConfig 
     </param-value> 
    </init-param> 
    </servlet> 
+0

为什么你要定义'AnnotationConfigWebApplicationContext'两次? – Braj 2015-03-31 08:57:43

0

Think @ M.Deinum是对的;通过远程处理和正常处理来分割你的bean。我做这在我的web.xml:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/classes/spring/root-context.xml</param-value> 
</context-param> 

<servlet> 
    <servlet-name>remoting</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/classes/spring/remoting-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>remoting</servlet-name> 
    <url-pattern>/remoting/*</url-pattern> 
</servlet-mapping> 

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

根context.xml中包含了我一切正常,豆(服务者,帮助者,计算器,JMS的听众,计划任务等)。

remoting-servlet.xml只指定那些需要通过HttpInvokerServiceExporter公开的服务。在根中定义的bean没有任何导入或链接,除了导出器的ref =“historyWebService”之外。

从我的理解,你最终得到2应用程序上下文:1根和1远程处理。远程处理从根继承了所有的bean,所以你不要声明或实例化bean两次(我认为)!我确定似乎没有重复生成的bean(即2个任务,2个jms听众等)。

+0

实际上,我使用注释进行配置。 – namtn 2015-03-31 10:34:51

+0

然后,2个xml文件可以通过适当的软件包进行简单的组件扫描。这样,你就可以对哪些bean加载到哪个上下文中保持一定程度的控制。 – shuttsy 2015-03-31 10:49:04

0

我有2个文件配置:

1. AppConfig: 

    @Configuration 
    @EnableScheduling 
    @EnableTransactionManagement 
    @EnableJpaRepositories("com.test.api.repository") 
    @PropertySource("classpath:application.properties") 
    public class AppConfig { 
      ............... 
    } 

2. WebInitializer 

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
    return new Class<?>[0]; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
    return new Class<?>[] { WebAppConfig.class }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
    return new String[] { "/" }; 
    } 

    @Configuration 
    @EnableWebMvc 
    @ComponentScan(basePackages = {"com.test.api"}) 
    public static class WebAppConfig extends WebMvcConfigurerAdapter { 

    ................... 
    } 
} 

在WebAppConfig,如果我改变@ComponentScan(basePackages = {"com.test.api"})到web包@ComponentScan(basePackages = {"com.test.api.web"}),所以春豆不是复制和调度无法运行两次。但有时它抛出异常:

error: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 
相关问题