2015-08-15 63 views
2

我無法理解Spring MVC配置和初始化。我不確定何時使用註釋以及何時不使用註釋!爲什麼我的spring上下文初始化了兩次?

我的應用程序由兩個戰爭模塊構成。我並不太在意在兩者中複製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 

爲什麼有4個memcached的連接,我不知道,但這是另一個問題一天....

爲了今天,我只是想弄清楚爲什麼我的背景下正在爲每個模塊初始化兩次......

web.xml中 - 擁有了AppConfig參考,併到webconfig在XML中的類。每個模塊都有單獨的web.xml文件。

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 

    <error-page> 
     <error-code>404</error-code> 
     <location>/WEB-INF/pages/errors/404.jsp</location> 
    </error-page> 
    <error-page> 
     <error-code>500</error-code> 
     <location>/WEB-INF/pages/errors/500.jsp</location> 
    </error-page> 

    <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.ocscommerce.admin.configuration.AppConfig</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <jsp-config> 
     <jsp-property-group> 
      <url-pattern>*.jsp</url-pattern> 
      <page-encoding>UTF-8</page-encoding> 
      <trim-directive-whitespaces>true</trim-directive-whitespaces> 

     </jsp-property-group> 
    </jsp-config> 

    <servlet> 
     <servlet-name>Spring MVC Dispatcher Servlet</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.ocscommerce.admin.configuration.WebConfig 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <filter> 
     <filter-name>encoding-filter</filter-name> 
     <filter-class> 
      org.springframework.web.filter.CharacterEncodingFilter 
     </filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 
    <filter-mapping> 
     <filter-name>encoding-filter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 


    <filter> 
     <display-name>springMultipartFilter</display-name> 
     <filter-name>springMultipartFilter</filter-name> 
     <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springMultipartFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <filter> 
     <display-name>springSecurityFilterChain</display-name> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>ERROR</dispatcher> 
     <dispatcher>FORWARD</dispatcher> 
     <dispatcher>REQUEST</dispatcher> 
    </filter-mapping> 
</web-app> 

然後在AppConfig類中,它會導入一些可能與問題無關的其他bean定義類。簽名是這樣的:

@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 { 
} 

然後WebConfig擴展WebMvcAdapter並具有這樣的成分:

@Configuration 
@EnableWebMvc 
@ComponentScan({"com.ocscommerce.admin", "com.ocscommere.services"}) 
@EnableTransactionManagement 
@EnableAspectJAutoProxy(proxyTargetClass = true) 
@EnableCaching 
public class WebConfig extends WebMvcConfigurerAdapter { 
} 

我想知道如果我有太多的組件掃描呢?

有人可以幫我修復這個配置嗎?

謝謝...

回答

1

我想你已經回答了你自己的問題,你在做同時在web配置和應用程序配置的組件掃描。

重構的組件掃描包名稱,以便在網絡配置只撿豆子的任何是相關的網絡層和應用程序配置只採摘您的應用程序豆類(如服務)。請記住,使用contextloaderlistener,webconfig將能夠看到應用程序配置中的bean。

+0

看到以前的答案,我從webconfig拿出部分掃描,但服務器不會啓動,因爲它抱怨從服務層缺失的自動裝配依賴。我使用Intellij,它抱怨它也沒有,我們已經檢查過類路徑。 –

+0

您不應該從'WebConfig'中刪除所有組件掃描,只能刪除特定於您的應用程序域的組件掃描。例如,'WebConfig'應該只掃描那些與Spring MVC相關的類,例如與Spring MVC或'@ Controller'類有關的'@ Configuration'。 Spring應該在調度程序上下文中創建這些對象。 (續...) –

+0

(...繼續)'AppConfig'應掃描與應用程序域相關的類,並且Spring應該在根上下文中創建這些對象。 Spring調度程序上下文繼承了根上下文,因此調度程序上下文中的對象(如控制器)可以訪問根上下文中的對象。 –

相關問題