🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
 @Configuration, @Bean, @Import, and @DependsOn 可以使用这些注释 1.10.1 @Component @Repository 和数据访问相关的。 @Component是通用类型, @Repository, @Service, and @Controller 是特定类型。 1.10.2 元注释 @Service和 @Component效果一样 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component // Spring will see this and treat @Service in the same way as @Component public @interface Service { // .... } } 1.10.3 自动侦测Bean @Configuration @ComponentScan(basePackages = "org.example") public class AppConfig { ... } } 使用XML的方式: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"/> </beans> 添加 context:component-scan需要包含<context:annotation-config> 使用ant时, 不要设置文件只读。 http://stackoverflow.com/questions/19394570/java-jre-7u45-breaks-classloader-getresources). Furthermore, the AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor are both included implicitly when you use the component-scan element. 1.10.4 使用过滤进行客制扫描 可以使用 include-filter和exclude-filter 进行过滤。 @Configuration @ComponentScan(basePackages = "org.example", includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"), excludeFilters = @Filter(Repository.class)) public class AppConfig { ... } } Filter Type Example Expression Description annotation (default) org.example.SomeAnnotation An annotation to be present at the type level in target components. assignable org.example.SomeClass A class (or interface) that the target components are assignable to (extend/implement). aspectj org.example..*Service+ An AspectJ type expression to be matched by the target components. regex org\.example\.Default.* A regex expression to be matched by the target components class names. custom org.example.MyTypeFilter A custom implementation of the org.springframework.core.type .TypeFilter interface. xml 中配置如下: <beans> <context:component-scan base-package="org.example"> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> </beans> 1.10.5 定义Bean的元数据 1.10.6 命名自动检测的组件 @Service("myMovieLister") public class SimpleMovieLister { // ... } } 自己定义bean name规则 @Configuration @ComponentScan(basePackages = "org.example", nameGenerator = MyNameGenerator.class) public class AppConfig { ... } } <beans> <context:component-scan base-package="org.example" name-generator="org.example.MyNameGenerator" /> </beans> 1.10.7 自动侦测组件的范围 默认是singleton @Scope("prototype") @Repository public class MovieFinderImpl implements MovieFinder { // ... } } 也可以定制范围 1.10.8 使用注释qualifier 元数据 @Component @Qualifier("Action") public class ActionMovieCatalog implements MovieCatalog { // ... } } qualifier 查找合适的bean 1.10.9 生成候选组件的索引 创建索引, 可以加快搜索的速度。 mvaen 需要加上: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> <version>5.0.8.RELEASE</version> <optional>true</optional> </dependency> </dependencies> 将会产生META-INF/spring.components 文件。