都扫描
默认的情况就是都扫描。
bean1.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" 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.xsd"> <!--开启组件扫描 1、如果要扫描多个包,多个包用逗号隔开 2、直接写到上层目录 所有类所有注解都扫描 --> <context:component-scan base-package="com.Keafmd"></context:component-scan> </beans>
设置扫描哪些内容
context:include-filter 设置扫描哪些内容
bean1.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" 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.xsd"> <!-- use-default-filters="false" 表示现在不使用默认fifter,自己配置fifter context:include-filter 设置扫描哪些内容 下面这个代表只扫描 Controller注解 --> <context:component-scan base-package="com.Keafmd" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
设置不扫描哪些内容
context:exclude-filter 设置不扫描哪些内容。
bean1.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" 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.xsd"> <!-- context:exclude-filter 设置不扫描哪些内容 下面这个代表 不扫描 Controller注解,其它的扫描 --> <context:component-scan base-package="com.Keafmd"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
以上就是组件扫描配置的内容。