<context:component-scan base-package=" " />

Original link: http://www.cnblogs.com/yehao1210/p/8081007.html

<context:component-scan base-package=" " />

   After this tag xml configuration, spring automatically to scan the following base-pack or sub-packets following java file, if the class has to scan these annotations @Component @ Controller @ Service or the like, put these classes registered as bean

Note: If you configure the <context: component-scan> then <context: annotation-config /> tag can no longer xml configuration because the former includes the latter.

Further <context: component-scan> also provides two sub-tabs

1.        <context:include-filter>

2.       <context:exclude-filter>

In the description of these two sub-label before, first talk about <context: component-scan> have a use-default-filters property, change the default attribute is true, which means that scans all of the standard package has designated under the @Component class, and registered as a bean. @Component is sub-notes @ Service, @ Reposity and so on. So if only so written in the configuration file

<context:component-scan base-package="tv.huan.weisp.web"/>

 Use-default-filter is true that at this time will all be at the java class or sub-base-package bag package scanning and matching the java class registered as bean. This scan can be found granularity bit too much, if you want to specify the following Controller scan package, how to do? At this time, the sub-label <context: incluce-filter> valor serves as a ground. As follows

<context:component-scan base-package="tv.huan.weisp.web .controller">  

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

</context:component-scan>  

This will only scan for the java class @Controller in the base-package designated and registered as bean

But because the use-dafault-filter does not specify above, it is by default true, so when the above configuration is changed as shown below and they will produce (note change in base-package package worth) and you expect inconsistent results

<context:component-scan base-package="tv.huan.weisp.web">  

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

</context:component-scan>  

At this time, spring only scanned @Controller, java class designation also scan the package is located under the sub-packets of the service package annotations @Service

Include-filter specified at this time not play a role, as long as the use-default-filter disposed on it to false. This can be avoided in the base-packeage configure multiple package names this is not a very elegant way to solve this problem.

Also in the project I was involved in base-package can be found in the package specified in sub-package does not contain any notes, so do not scan, then you can specify <context: exclude-filter> to filter, this package does not You need to be scanned. Based on the above description

Case Use-dafault-filters = "false" is: <context: exclude-filter> is not specified scan, <context: include-filter> specified scan

 

<! - access to static resource files ->

Is commonly used in SpringMVC the Controller and View. However, we often need to access static resources, such as html, js, css, image and so on.

The default access URL will be intercepted DispatcherServlet, but we hope to have direct access to static resources

<mvc:resources location="/css/" mapping="/css/**" />

<mvc:resources location="/images/" mapping="/images/**" />

<mvc:resources location="/js/" mapping="/js/**" />

in web.xml

<servlet>
  <servlet-name>SpringMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>

      classpath: springmvc-dispatcher.xml (or /WEB-INF/springmvc-dispatcher.xml)

    </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

Reproduced in: https: //www.cnblogs.com/yehao1210/p/8081007.html

Guess you like

Origin blog.csdn.net/weixin_30328063/article/details/94785534