基于注解注册组件
包扫描@ComponentScan
@Componnet Scan相当于的在xml中<context:componet-scan base-package="">
- @ComponentScan(value="xxx.xxx")配置后,在配置的包下所有配置了组件注解的类都会被注册到容器中
//定义包扫描,其中value的值要为包名
@ComponentScan(value="xxx.xxx")
public class Mainconfig {}
//将要注册的类标注
@Component
class BeRegisted {}
//测试方法
@Test
void testComponent(){
ApplciationContext ctx =
new AnnotationConfigContextApplication(Mainconfig.class);
assertThat( ctx.containsBean("xxx.xxx.BeRegisted")).isTrue;
}
includeFilter,excludeFilter属性
//只扫描带有@Controller的组件
@Componnent(value="xxx.xxx",includeFilter(@Filter(value=FilterType.ANNOTATION,classes={Controller.class})),userDefaultFilers = false)
public class Config{}
//不注册@Controller组件
@ComponentScan(value="xxx.xxx",excludeFileter={@Fileter(value=FilterType.ANNOTATION,classes={Controller.class})})
public clss Conifg{}