@Component
1. 定义:注解类、接口、注解、枚举。
@Target(ElementType.TYPE) /** Class, interface (including annotation type), or enum declaration */ @Retention(RetentionPolicy.RUNTIME)
2. spring 文档说明:
The @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository.
@Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases.
3. Automatically detecting classes and registering bean definitions 自动检测类和注册bean定义
3.1 @Configuration和@ComponentScan 联合使用,标明要扫描的包及其子包。
@Configuration @ComponentScan(basePackages = "org.example") public class AppConfig { ... }
可以简化为 @ComponentScan("org.example")。
也可以写多个包,用(,|;| )分隔,如@ComponentScan("org.example,com,example")
3.2 使用filter 定制扫描
@Configuration @ComponentScan(basePackages = "org.example", includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"), excludeFilters = @Filter(Repository.class)) public class AppConfig { ... }
浙公网安备 33010602011771号