?@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. 附錄