spring的注解扫描有两种:
- 很常见的一种就是使用application.xml进行扫描,扫描当前包下面的所有类。
<context:component-scan base-package="org.youyuan.bean"></context:component-scan>
2.也可以通过java代码进行扫码,定义一个配置类
@Configuration
@ComponentScan(basePackages = "org.youyuan.service")
public class UserConfig {
}
这个配置类的作用相当于上面的component-scan的作用,其中@ComponentScan的basePackages表示扫描的包的位置。
当然还有另外一种扫面方式,通过扫描注解。
@Configuration
@ComponentScan(basePackages = "org.youyuan.service",useDefaultFilters = true,excludeFilters =
{@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class UserConfig {
}
表示扫描当前org.youyuan.service下所有的Bean,除了Controller。