@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 文件。
- 空白目录
- 0.环境准备
- 0.1基于maven的工程创建
- 1.控制反转容器
- 1.1 Spring控制反转容器和beans介绍
- 1.2 容器概览
- 1.3 Bean概览
- 1.4 依赖
- 1.5 Bean的范围
- 1.6 客制bean的特性
- 1.7 Bean定义的继承
- 1.8 容器扩展点
- 1.9 基于注解的容器配置
- 1.10 类路径扫描及组件管理
- 1.11 使用JSR 330标准的注解
- 1.12 基于Java的容器配置
- 1.12.1 基本概念: @Bean 和 @Configuration
- 1.13 环境抽象化
- 1.14 注册一个LoadTimeWeaver
- 1.15 ApplicationContext的附加功能
- 1.16 BeanFactory
- 2. 资源
- 3. 验证,数据绑定和类型转换
- 4. Spring表达式语言(SpEL)
- 5. Spring面向方面的切面编程
- 6. Spring AOP 接口
- 7. 空安全
- 8. 数据缓冲和编码
- 9. 附录