问题描述:在公司的项目中进行单元测试的时候发现,即使这个类上没有标注@Comment或者@Service等这一类的注解,但是Bean依然能够注入进来。
按理说springboot在进行扫描的时候,没有标注注解的类是不会加载到ioc容器里面,就很奇怪。因为常见注入类到ioc容器中,无非通过注解和xml,注解没配置,那应该就是通过xml实例化了,经过排查发现,在启动类中通过@ImportResource导入了xml文件applicationContext.xml在这个xml中有这样一段配置
<context:component-scan base-package="com.xxx.xxx">
<context:include-filter type="aspectj"
expression="com.xxx.xxx..impl..*BOImpl"/>
<context:include-filter type="aspectj"
expression="com.xxx.xxx..impl..*ServiceImpl"/>
</context:component-scan>
context:include-filter过滤器有五种type:
assignable-指定扫描某个接口派生出来的类
annotation-指定扫描使用某个注解的类
aspectj-指定扫描AspectJ表达式相匹配的类
custom-指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类
regex-指定扫描符合正则表达式的类
所以在当前expression设置的路径下的类都会自动注入到容器中