applicationContext nie znajduje kontrolerów dla kontekstu serwletu

Mam aplikację internetową Spring z aplikacją applicationContext.xml i konfiguracją dispatcher-servlet.xml. Zdefiniowałem<context:component-scan /> w applicationContext.xml, ale gdy uruchomię aplikację, kontrolery nie zostaną znalezione, chyba że dodam<context:component-scan /> do dispatcher-servlet.xml. Używam tego samego pakietu podstawowego w obu, więc to nie jest problem.

Jestem zdezorientowany, ponieważ jamyśl że applicationContext.xml był rodzicem dispatcher-servlet.xml. Nie wkładam<context:component-scan /> w pliku applicationContext.xml wystarczy?

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

EDYCJA: Używam również mvc: sterowany adnotacjami w pliku dispatcher-servlet.xml, który ma odbierać Kontrolery (pomyślałem?).

EDYCJA 2: Oto pliki konfiguracyjne. Usunąłem kilka ustawień Spring Security i OAuth z applicationContext.xml (ze względów bezpieczeństwa i prawdopodobnie nie są one istotne).

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
      http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />

</beans>

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo.controller" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="2" />
</bean>

<bean id="contentViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="defaultViews">
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </property>
    <property name="order" value="1" />
</bean>

</beans>

EDIT 3: Dobra, to jest interesujące. Moje usługi i klasy dao są w innym projekcie (JAR), który odwołuję się do projektu internetowego. Używam konfiguracji opartej na java i odwołuję się do niej z pliku applicationContext.xml:

<bean class="bar.foo.config.ServicesConfig" />

Oznacza to, że w moim projekcie WWW są tylko adnotacje kontrolera (gdzie znajduje się applicationContext.xml). Z perspektywy czasu usunięcie kontekstu: skanowanie składników z mojego pliku applicationContext.xml nie powinno mieć żadnego wpływu, ponieważ nie ma adnotacji z wyjątkiem adnotacji @Controller (FIX to EDIT: istnieją adnotacje @Autowired). Ale kiedy usuwam kontekst: skanowanie składników z applicationContext.xml, mówi, że kontrolery (znalezione ze skanowania serwletu dyspozytora) nie mogą znaleźć moich klas usługi. Czy odniesienie do ServicesConfig nie powinno wystarczyć? Oto klasa ServicesConfig dla referencji - ma ona własne skanowanie komponentów dla Usług, które są innym pakietem niż skanowanie aplikacji applicationContext.xml.

@Configuration
@ComponentScan({ "some.other.package", "another.package" })
@ImportResource({ "classpath:commonBeans.xml" })
@PropertySource({ "classpath:services.properties",
"classpath:misc.properties" })
public class ServicesConfig {
  // Bean definitions //
}

ROZWIĄZANIE:

Kiedy usunąłem kontekst: skanowanie komponentów z mojego kontekstu root, Kontrolerzy nie odbierali fasoli usług autoworków. Dzieje się tak, ponieważ kontekst roota odwołuje się do mojego komponentu konfiguracyjnego opartego na java, ale nie miałem konfiguracji kontekstowej roota do skanowania w poszukiwaniu komponentów. Dlatego, gdy dodaję skanowanie składników do kontekstu głównego (applicationContext.xml), wszystko działa. Oto, co mam teraz:

applicationContext.xml:

<bean class="bar.foo.config.ServicesConfig" />
<context:component-scan base-package="bar.foo.config" />

dispatcher-servlet.xml:

<context:component-scan base-package="bar.foo.controller" />

Mam konfigurację kontekstu internetowego do pobrania kontrolera, Autowired i wszelkich innych adnotacji w pakiecie kontrolera - nie jestem pewien, czy jest to najlepsza praktyka, czy nie.

questionAnswers(3)

yourAnswerToTheQuestion