spring容器能自动装配bean的依赖关系.自动装配的优点如下:
* 显著减少指定属性和构造参数的需要
* 随着对象的变化自动更新配置,例如,需要在一个类中添加依赖,这个依赖可以自动满足而不需要修改配置项
基于xml的配置,使用元素`<bean>`的属性`autowire`,自动装配有4中模式可供选择,如下:
| 模式 | 说明 |
| --- | --- |
| no | 默认不开启自动状态 |
| byName | 按属性名称自动装配,spring会寻找set属性的方法 |
| byType | 按属性类型自动状态,如果容器中有属性的 |
|constructor|类似byType,但是适用于构造参数,如果没有指定bean作为参数的构造方法,会引起严重错误|
## Limitations and disadvantages of autowiring
自动装配的一些限制和缺点:
* `property` 和 `constructor-arg`属性的设置会覆盖自动装配.不能对基本数据类型和`String,Classes`自动装配,这在设计上就是限制的
* 自动装配没有精确装配准确,spring管理的对象之间的依赖关系不再明确记录
* 从spring容器生成文档的工具,装配信息不再有效
* spring管理的bean可能会有多个都满足自动装配的属性或构造参数,对于集合未必会出错,但是对于单一的值,就会造成混淆,导致出错,
## Excluding a bean from autowiring
你可以排除bean的自动装配,设置元素`<bean/>`的属性`autowire-candidate`为`false`,自动装配的机制对此bean无效(包括注解[@Autowired](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#beans-autowired-annotation]))
>`autowire-candidate`属性只对byType的自动装配起作用,对于byName不起作用,也就是说,如果name匹配还是会自动装配
>
你也可以限制自动装配基于名字的正则匹配,顶级元素`<beans/>`的属性`default-autowire-candidates`可以设置一个或多个正则模式,例如,对于name以Repository结尾的bean自动装配,则取值为 *Repository.多个模式,用逗号隔开.对于bean的属性`autowire-candidate`总是优先的,匹配模式对这些bean不起作用.
这项技术,对于不想把bean自动装配到另一个bean是非常有用的.这并不意味bean本身不能使用自动装配,只是本身不会自动装配到其他bean中去.
- 正确打开本书的姿势
- 第一部分 Core
- 1. Ioc container
- 1.1. Introduction to the Spring IoC container and beans
- 1.2. Container overview
- 1.2.1. Configuration metadata
- 1.2.2. Instantiating a container
- 1.2.3. Using the container
- 1.3. Bean overview
- 1.3.1. Naming beans
- 1.3.2. Instantiating beans
- 1.4. Dependencies
- 1.4.1. Dependency Injection
- 1.4.2. Dependencies and configuration in detail
- 1.4.3. Using depends-on
- 1.4.4. Lazy-initialized beans
- 1.4.5. Autowiring collaborators
- 1.4.6. Method injection
- 1.5 Bean Scopes
- 1.6. Customizing the nature of a bean TODO
- 1.7. Bean definition inheritance TODO
- 1.8. Container Extension Points TODO
- 1.9. Annotation-based container configuration
- 1.9.1. @Required
- 1.9.2. @Autowired
- 1.9.3. Fine-tuning annotation-based autowiring with @Primary
- 1.9.4. Fine-tuning annotation-based autowiring with qualifiers TODO
- 1.9.5. Using generics as autowiring qualifiers TODO
- 1.9.6. CustomAutowireConfigurer TODO
- 1.10. Classpath scanning and managed components
- 1.10.1. @Component and further stereotype annotations
- 1.11. Using JSR 330 Standard Annotations TODO
- 1.12. Java-based container configuration
- 1.12.1. Basic concepts: @Bean and @Configuration
- 1.12.2. Instantiating the Spring container using AnnotationConfigApplicationContext
- 2. Resources
- 2.1. Introduction
- 2.2. The Resource interface
- 2.3. Built-in Resource implementations
- 2.3.1. UrlResource
- 2.3.2. ClassPathResource
- 2.3.3. FileSystemResource
- 2.3.4. ServletContextResource
- 2.3.5. InputStreamResource
- 2.3.6. ByteArrayResource
- 2.4. The ResourceLoader
- 2.5. The ResourceLoaderAware interface
- 2.6. Resources as dependencies
- 2.7. Application contexts and Resource paths
- 2.7.1. Constructing application contexts
- 2.7.2. Wildcards in application context constructor resource paths
- 2.7.3. FileSystemResource caveats
- 3. Validation, Data Binding, and Type Conversion
- 4. Spring Expression Language (SpEL)
- 5. Aspect Oriented Programming with Spring
- 5.1. Introduction
- 5.1.1. AOP concepts
- 5.1.2. Spring AOP capabilities and goals
- 5.1.3. AOP Proxies
- 5.2. @AspectJ support
- 5.2.1. Enabling @AspectJ Support
- 5.2.2. Declaring an aspect
- 5.2.3. Declaring a pointcut
- 5.2.4. Declaring advice
- 5.2.5. Introductions TODO
- 5.2.6. Aspect instantiation models TODO
- 5.2.7. Example
- 5.3. Schema-based AOP support TODO
- 5.4. Choosing which AOP declaration style to use TODO
- 5.5. Mixing aspect types TODO
- 5.6. Proxying mechanisms
- 5.6.1. Understanding AOP proxies
- 5.7. Programmatic creation of @AspectJ Proxies
- 5.8. Using AspectJ with Spring applications
- 5.8.1. Using AspectJ to dependency inject domain objects with Spring
- 5.8.2. Other Spring aspects for AspectJ
- 第二部分 Testing
- 第三部分 Data Access
- 1. Transaction Management
- 1.1. Introduction to Spring Framework transaction management
- 1.2 Advantages of the Spring Framework’s transaction support model
- 1.2.1. Global transactions
- 1.2.2. Local transactions
- 1.2.3. Spring Framework’s consistent programming model
- 1.3. Understanding the Spring Framework transaction abstraction
- 1.4. Synchronizing resources with transactions
- 1.4.1. High-level synchronization approach
- 1.4.2. Low-level synchronization approach
- 1.4.3. TransactionAwareDataSourceProxy
- 1.5. Declarative transaction management
- 1.5.1. Understanding the Spring Framework’s declarative transaction implementation
- 1.5.2. Example of declarative transaction implementation
- 1.5.3. Rolling back a declarative transaction
- 1.5.4. Configuring different transactional semantics for different beans
- 1.5.5. tx:advice元素的 settings
- 1.5.6. Using @Transactional
- 1.5.7. Transaction propagation
- 1.5.8. Advising transactional operations
- 1.5.9. Using @Transactional with AspectJ TODO
- 第四部分 web servlet
- 第五部分 Web Reactive
- 第六部分 Integration
- 第七部分 Languages