最近在学习Springmvc配置文件,以下是个人对它的一些理解
主要说一下关于注解的问题:
配置注解需要在XML文件头中加入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" <-----就是context和下面的两个context
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
然后进行<context:component-scan base-package="" use-default-filters="true">标签的配置
这个标签中有两个子标签分别是
<context:include-filter type="annotation" expression=""/>
<context:exclude-filter type="annotation" expression=""/>
当你在expression中写上org.springframework.stereotype.Controller这个时候分别代表包含@Controller和不包含@Controller
但是!!!
因为<context:component-scan>中有一个use-default-filters属性,它默认是true的,而当他为true时还会扫描base-package中的@Component以及它的子注解@Service,@Reposity
这样就和我们一般开发相违背,我们只要扫描有@Controller的话,就需要把true设置成false
下面是我的只扫描Controller类的例子
<context:component-scan base-package="org.zdc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
注意:在这里我的org.zdc的后面还会包含其他的包名
当然,如果你把所有的Controller类放在了同一个web下,那么你就不用再设置这个属性了,比如这样
<context:component-scan base-package="org.zdc.web"/>
因为你的web下全都是只含有@Controller的注解,所以他也不会扫描其他的目录了.