Reputation: 3859
I have created a spring applications based on tutorials, and there is something that isnt completely clear to me that I hope someone can clear up.
I have 2 config files - mvc-config.xml which is for the dispatcher servlet and application-config.xml which is for the beans which make up the application.
If I want to use annotations in both controllers and my beans (daos and services) do I need to include the following in both xml files or do these things get inherited?
<annotation-driven />
<context:component-scan base-package="com.ws.jaxb" />
<context:annotation-config />
Upvotes: 0
Views: 285
Reputation: 64079
What happens when you setup Spring to use both mvc-config.xml
and application-config.xml
is that two application contexts are created. The root context (corresponding to application-config.xml
) and the web context (corresponding to mvc-config.xml
).
In your case what you need to do is something like the following to get everything to work as expected:
mvc-config.xml
<mvc:annotation-driven /> <!-- for standard Spring MVC features -->
<context:component-scan base-package="com.ws.jaxb" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
application-config.xml
<context:component-scan base-package="com.ws.jaxb">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
The code mentioned above has the effect to add controllers only to the web context while all the rest of the Spring beans are added to the root context.
Also note that <context:annotation-config/>
is not needed since <context:component-scan>
provides a superset of the functionality
Upvotes: 1