Spring中component-scan的exclude-filter和include-filter

1、Spring-SpringMVC项目中一般都会把spring-application.xml (Spring配置文件)和spring-controller.xml(SpringMVC配置文件)进行分开配置。

在spring-application.xml 只对一些事务逻辑的注解扫描:@Component、@Repository、@Service

在spring-controller.xml中只对控制器注解扫描:@Controller、@RestController、@ControllerAdvice

2、了解component-scan的几个属性

base-packages:Spring将扫描的基础package名,Spring会扫描该包以及其子孙包下的所有类

use-defaultFilters:默认为true,此时Spring扫描类时发现如果其被标注为 @Component、@Repository、@Service、@Controller则自动实例化为bean并将其添加到上下文中,如果设置为false,即使将其标注为@Component或者其他,Spring都会忽略

include-filters: 指定扫描时需要实例化的类型,我们可以从名字看到这是一个Filter,你可以自己定义该Filter,Spring为我们提供了一套方便的实现,我们可以根据标注、类、包等相关信息决定当扫描到该类时是否需要实例化该类,需要注意的是如果你仅仅想扫描如@Controller不仅要加includeFilters,还需要将useDefaultFilters设置为false

exclude-filter:指定扫描到某个类时需要忽略它,实现和上一个Filter一样,区别只是如果Filter匹配,Spring会忽略该类

这样includeFilters以及excludeFilterF的行为就很清楚了,Spring每扫描一个类,都会经过includeFilters以及excludeFilters,如果某个Filter匹配,就执行相应的操作(实例化或者忽略)

3、spring-application.xml 以及spring-controller.xml自动扫描注解的配置实例

spring-application.xml:

<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com.zx.stlife">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
    <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

spring-controller.xml:

<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.zx.stlife.controller" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
 <!--还可以配置拦截器 等一些和访问路径有关系的 -->

 

============================================

附:

<context:annotation-config />

  1. 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
  2. 如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
  3. 如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
  4. 如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

而使用<context:annotation-config />就可以隐式地自动向Spring容器注册4个BeanPostProcessor:

  • AutowiredAnnotationBeanPostProcessor
  • CommonAnnotationBeanPostProcessor
  • PersistenceAnnotationBeanPostProcessor
  • RequiredAnnotationBeanPostProcessor

 

<mvc:annotation-driven>

替我们自动配置最新版的注解的处理器映射器和处理器适配器

  1. 注解式处理器映射器,对类中@RequestMapping的方法进行映射,根据@RequestMapping定义的url进行匹配,匹配成功之后返回Handler对象给前端控制器。从Spring3.1版本开始,废除了DefaultAnnotationHandlerMapping,推荐使用RequestMappingHandlerMapping完成注解式处理器映射器。
  2. 注解式的处理器适配器,从Spring3.1版本开始,废除了AnnotationMethodHandlerAdapter,推荐使用RequestMappingHandlerAdapter完成注解式处理器映射器。
 <!-- 如果没有显式的配置处理器映射器和处理器适配那么springMvc会去默认的dispatcherServlet.properties中查找,
        对应的处理器映射器和处理器适配器去使用,这样每个请求都要扫描一次他的默认配置文件,
        效率非常低,会降低访问速度,所以要显式的配置处理器映射器和
        处理器适配器 -->
        
        <!-- 注解形式的处理器映射器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
        <!-- 注解形式的处理器适配器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
        
        <!-- 配置最新版的注解的处理器映射器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
        <!-- 配置最新版的注解的处理器适配器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->

	<!-- 注解驱动:
		作用:替我们自动配置最新版的注解的处理器映射器和处理器适配器
	 -->
	<mvc:annotation-driven></mvc:annotation-driven>

当我们需要controller返回一个map的json对象时,可以设定<mvc:annotation-driven />,

同时设定<mvc:message-converters> 标签,设定字符集和json处理类,例如:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

该转换器主要用于请求和响应消息的转换。

conversion-service: 自定义转换器并绑定在处理器映射器上

public class DateConverter implements Converter<String, Date> {
	@Override
	public Date convert(String sources) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(sources);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
    <mvc:annotation-driven conversion-service="conversionService" />
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.xinboedu.utils.DateConverter"></bean>
			</set>
		</property>
	</bean>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值