转载地址:https://blog.csdn.net/qwe5810658/article/details/74343228java
一般状况下咱们在建立spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,若是扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是adc.正则表达式
注:若是配置了<context:component-scan>, 那么<context:annotation-config/>标签就能够不用在xml中再配置了, 由于前者包含了后者。另外<context:annotation-config/>还提供了两个子标签 <context:include-filter>和 <context:exclude-filter>
在注解注入以前也必须在spring的配置文件中作以下配置,咱们看下spring.xml文件的内容:spring
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.sparta.trans" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
这个配置文件中必须声明xmlns:context 这个xml命名空间,在schemaLocation中须要指定schema:express
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
这个文件中beans根节点下只有一个context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters=”false”表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含@Service,@Component,@Repository,@Controller注解修饰的类,use-default-filters属性的默认值为true,这就意味着会扫描指定包下标有@Service,@Component,@Repository,@Controller的注解的所有类,并注册成bean。
因此若是仅仅是在配置文件中写<context:component-scan base-package="com.sparta.trans"/>
Use-default-filter此时为true时,那么会对base-package包或者子包下全部的java类进行扫描,并把匹配的java类注册成bean。spa
因此这用状况下能够发现扫描的力度仍是挺大的,可是若是你只想扫描指定包下面的Controller,那该怎么办?此时子标签<context:incluce-filter>
就能够发挥做用了。以下所示.net
context:component-scan base-package="com.sparta.trans.controller"> <context:include-filter type="regex" expression="com\.sparta\.trans\.[^.]+(Controller|Service)"/> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> </context:component-scan>
这样就会只扫描base-package指定下的有@Controller下的Java类,并注册成beancode
注: context:component-scan节点容许有两个子节点和。filter标签的type和表达式说明以下:component
Filter Type | Examples Expression | Description |
---|---|---|
annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class |
assignable | org.example.SomeClass | 指定class或interface的全名 |
aspectj | org.example..*Service+ | AspetJ语法 |
regex | org.example.Default.* | Regelar Expression |
custom | org.example.MyTypeFilter | Spring3新增自订Type,称做org.springframework.core.type.TypeFilter |
在咱们的示例中,将filter的type设置成了正则表达式,regex,注意在正则里面.表示全部字符,而.才表示真正的.字符。咱们的正则表示以Controller或者Service结束的类。
咱们也可使用annotaion来限定,如上面注释掉的所示。这里咱们指定的include-filter的type是annotation,expression则是注解类的全名。xml
可是由于use-dafault-filter在上面并无指定,默认就为true,因此当把上面的配置改为以下所示的时候,就会产生与你指望相悖的结果(注意base-package包值得变化)blog
<context:component-scan base-package="com.sparta.trans"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
此时,spring不只扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类
此时指定的include-filter没有起到做用,只要把use-default-filter设置成false就能够了。这样就能够避免在base-packeage配置多个包名来解决这个问题了。
另外在实际项目开发中咱们能够发如今base-package指定的包中有的子包是不含有注解的,因此不用扫描,此时能够指定来进行过滤,说明此包不须要被扫描。因此综上能够看出 use-dafault-filters=”false”的状况下:能够指定不须要扫描的路径来排除扫描这些文件,能够指定须要扫描的路径来进行扫描。可是因为use-dafault-filters的值默认为true,因此这一点在实际使用中仍是须要注意一下的。
@Service告诉spring容器,这是一个Service类,标识持久层Bean组件,默认状况会自动加载它到spring容器中。
@Autowried注解告诉spring,这个字段须要自动注入
@Scope指定此spring bean的scope是单例
@Repository注解指定此类是一个容器类,是DA层类的实现。标识持久层Bean组件
@Componet:基本注解,标识一个受Spring管理的Bean组件
@Controller:标识表现层Bean组件
base-package属性告诉spring要扫描的包 use-default-filters=”false”表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Responsitory,Controller注释修饰类。