Spring上下文加载程序
我有一个关于spring上下文的问题。我的应用程序正在使用spring和spring调度程序。 在web.xml中,我声明:Spring上下文加载程序,spring,scheduler,applicationcontext,Spring,Scheduler,Applicationcontext,我有一个关于spring上下文的问题。我的应用程序正在使用spring和spring调度程序。 在web.xml中,我声明: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 我的问题是: 如果我在web.xml中声明org.springframework.web.cont
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我的问题是:
如果我在web.xml中声明org.springframework.web.context.ContextLoaderListener,调度程序将运行两次,所有bean都是重复的,应用程序启动时间约为160秒
如果我删除org.springframework.web.context.ContextLoaderListener,
spring引发异常:未找到WebApplicationContext:未注册ContextLoaderListener。应用程序启动时间减少到80秒
我怎样才能解决它?谢谢大家 谢谢@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>
认为@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>
root-context.xml包含我所有的普通bean服务、助手、计算器、jms侦听器、计划任务等
remoting-servlet.xml仅指定需要通过HttpInvokerServiceExporter公开的服务。除了导出器的ref=historyWebService之类的内容外,根目录中没有定义到bean的导入或链接
据我所知,您最终得到了2个应用程序上下文:1个root和1个remoting。远程处理一个从根目录继承所有bean,所以我认为您不会再次声明或实例化bean!!!我确定没有生成重复的bean,即2个任务、2个jms侦听器等。我有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 {
...................
}
}
拆分您的配置。不要让ContextLoaderListener和DispatcherServlet加载相同的配置文件。如果你这样做,所有的东西都会被加载两次。你为什么还要考虑使用XML配置呢?现在是2015年-为什么要定义两次AnnotationConfigWebApplicationContext?事实上,我正在使用annotation进行配置。然后这两个xml文件可以通过适当的包进行简单的组件扫描。这样,您就可以在一定程度上控制将哪些bean加载到哪个上下文中。
error: org.hibernate.LazyInitializationException: could not initialize proxy - no Session