Spring mvc 为什么我的spring上下文要初始化两次?

Spring mvc 为什么我的spring上下文要初始化两次?,spring-mvc,Spring Mvc,我无法理解SpringMVC的配置和初始化。我不确定什么时候使用注释,什么时候不使用注释 我的应用程序分为两个war模块。我不太关心在这两个容器中复制bean容器。但在启动时,两个模块都在两次初始化spring上下文,正如从启动时的其中一个模块获取的日志所示: [2015-08-15 03:49:51,416] Artifact frontend:war exploded: Deploy took 27,529 milliseconds 15:50:00.051 DEBUG com.ocscom

我无法理解SpringMVC的配置和初始化。我不确定什么时候使用注释,什么时候不使用注释

我的应用程序分为两个war模块。我不太关心在这两个容器中复制bean容器。但在启动时,两个模块都在两次初始化spring上下文,正如从启动时的其中一个模块获取的日志所示:

[2015-08-15 03:49:51,416] Artifact frontend:war exploded: Deploy took 27,529 milliseconds
15:50:00.051 DEBUG com.ocscommerce.admin.configuration.WebConfig 53 <init> - Initialising WebConfig
15:50:06.409 DEBUG com.ocscommerce.admin.configuration.AppConfig 48 <init> - Loading application context
15:50:06.424 DEBUG com.ocscommerce.admin.configuration.SecurityConfig 54 <init> - Loading security config.
15:50:07.327 DEBUG com.ocscommerce.services.configuration.SolrConfig 43 <init> - Loading SOLR Config.
2015-08-15 15:50:08.194 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-08-15 15:50:08.200 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
15:50:11.269 DEBUG com.ocscommerce.admin.configuration.WebConfig 53 <init> - Initialising WebConfig
15:50:13.498 DEBUG com.ocscommerce.admin.configuration.AppConfig 48 <init> - Loading application context
15:50:13.503 DEBUG com.ocscommerce.admin.configuration.SecurityConfig 54 <init> - Loading security config.
15:50:13.999 DEBUG com.ocscommerce.services.configuration.SolrConfig 43 <init> - Loading SOLR Config.
2015-08-15 15:50:14.336 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-08-15 15:50:14.337 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
然后,WebConfig扩展WebMvcAdapter,并具有如下组件:

@Configuration
@PropertySource(value = {"classpath:application.properties"})
@EnableScheduling
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableTransactionManagement
@ComponentScan({"com.ocscommerce.admin", "com.ocscommerce.services"})
@Import(value = {SolrConfig.class, SecurityConfig.class, PersistenceConfig.class})
public class AppConfig {
}
@Configuration
@EnableWebMvc
@ComponentScan({"com.ocscommerce.admin", "com.ocscommere.services"})
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
public class WebConfig extends WebMvcConfigurerAdapter {
}
我想知道是否有太多的组件扫描

有人能帮我修复这个配置吗


谢谢…

我想您已经回答了自己的问题,您正在web配置和应用程序配置中进行组件扫描


重构组件扫描中的包名称,以便web配置只拾取与web层相关的任何bean,而应用配置只拾取应用程序bean(例如服务)。请记住,使用contextloaderlistener,webconfig将能够看到应用程序配置中的bean。

请参阅前面的答案,我从webconfig中取出了组件扫描,但服务器不会启动,因为它抱怨缺少来自服务层的autowire依赖项。我使用Intellij,它也抱怨它丢失了,我们已经检查了类路径。您不应该从
WebConfig
中删除所有组件扫描,只删除特定于您的应用程序域的组件扫描
WebConfig
应仅扫描与Spring MVC相关的类,例如与Spring MVC相关的
@Configuration
@Controller
类。Spring应该在dispatcher上下文中创建这些对象。(续…(…续)
AppConfig
应扫描与应用程序域相关的类,Spring应在根上下文中创建这些对象。Spring dispatcher上下文继承根上下文,因此dispatcher上下文中的对象(如控制器)可以访问根上下文中的对象。
@Configuration
@EnableWebMvc
@ComponentScan({"com.ocscommerce.admin", "com.ocscommere.services"})
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
public class WebConfig extends WebMvcConfigurerAdapter {
}