The scope <context: component-scan ... / "> and <context: property-placeholder ... /"> in hierarchical contexts
I read:
What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?
@RequestMapping annotation does not work if <context: component-scan /> in the application context instead of the dispatcher context (more on this later)
and several others, but not one of them answers the question:
Why is a scope <context:component-scan.../>limited if it is present in the ROOT context of a Spring MVC application?
, โโ beans, @Component (@Repository, @Service @Controller).
:
applicationContext.xml ( )
<beans...>
...
<context:component-scan base-package="com.myproject"/>
<context:property-placeholder
ignore-resource-not-found="true"
location="classpath:default.properties, file:///etc/gallery/gallery.properties"/>
</beans>
main-servlet.xml ( )
<beans ...>
...
<mvc:annotation-driven/>
<mvc:resources mapping="/image/**" location="file:/${gallery.location}" />
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
...
</beans>
/MyProject//MainController.java
package com.myproject.web;
@Controller
public class MainController
{
...
@RequestMapping("/gallery/**")
public String gallery(ModelMap modelMap, HttpServletRequest req, HttpServletResponse resp) throws IOException
{
...
}
}
Spring , beans, , . , <context:...> beans, . , , . BOTH <context:component-scan.../> <context:property-placeholder.../> .
<context:component-scan.../>
Sep 15, 2015 10:08:16 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/gallery/habitat/20150813] in DispatcherServlet with name 'main'
Sep 15, 2015 10:08:16 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/error] in DispatcherServlet with name 'main'
, @Controller .
<context:property-placeholder.../> @Value , , .
<context:.../> bean, , beans , . , component-scan, bean?
@RequestMapping, , < context: component-scan/ > , <mvc:annotation-driven /> , , component-scan.
"", , , , , - . , "" " โโ " .
<context:property-placeholder />
<context:property-placeholder /> PropertySourcesPlaceholderConfigurer, [BeanFactoryPostProcessor]. BeanFactory, BeanFactory (ApplicationContext - ). , BeanFactory, . . , .
2 <context:property-placeholder />, , . , <context:property-placeholder /> <util:properties />. bean , bean <context:property-placeholder /> properties-ref. .
<util:properties id="appProperties" location="classpath:default.properties, file:///etc/gallery/gallery.properties" ignore-resource-not-found="true" />
<context:property-placeholder properties-ref="appProperties" />
<context:property-placeholder properties-ref="appProperties" />
<mvc:annotation-driven />
<mvc:annotation-driven />, , , RequestMappingHandlerMapper, bean , @RequestMapping @Controller . ApplicationContext, NOT . detectHandlerMethodsInAncestorContexts true, .
, , , , , beans, .. ( DispatcherServlet - , @Controllers.).
<context:component-scan />, , ( ) . beans , .. , <tx:annotation-driven />, AOP AOP, BeanFactoryPostProcessor BeanPostProcessor, .
<context:component-scan base-package="com.myproject">
<context:exclude-filters type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:component-scan base-package="com.myproject" use-default-filters="false">
<context:include-filters type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>