[原文](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#beans-beanname)
[toc]
bean的定義本質上就是創建對象的清單。當詢問容器指定name的bean時,容器查詢清單,使用bean定義封裝的配置元數據來創建或獲取實際對象。
如果使用xml的配置元數據,則元素`<bean/>`的屬性class指定類型的對象實例.這個class屬性,其實是`BeanDefinition`類的內部屬性,通常是必需的(例外情況參考[工廠方法實例對象](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#beans-factory-class-instance-factory-method) 和[子類實例](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#beans-child-bean-definitions)).使用class的兩種方式:
* 典型的,容器通過反射直接調用構造方法創建對象,和直接使用java操作符`new`效果類似.
* 指定包含靜態工廠方法來創建對象的實際類,比較少用的場景,容器調用類的靜態工廠方法創建bean,這個bean可以是類本身實例,也可以是其他類實例
> *內部類命名*
> 對于靜態內部類的定義,需要使用二元名稱.例如在包`com.example`下有個類`Foo`,并且包含靜態內部類`Bar`,定義bean的class屬性的值就是:`com.example.Foo$Bar`.
>注意使用$字符來分割內部類和外部類
>
## Instantiation with a constructor
當你通過構造函數來創建bean時,所有普通類都能被spring使用和兼容.即,開發的類不需要實現特定接口或用特定方式編碼,只要指定bean的class就足夠了.然而,根據特定bean使用的ioc容器類型,你可能需要默認的無參構造方法.
spring 容器可以管理任何你想要它管理的類,不局限于純javaBeans.大多數spring使用者更喜歡無參構造函數的bean和恰當的get,set模式設置屬性.你的容器中也可以有非bean風格的類.例如,你需要使用一個傳統的絕對不符合javaBean規范的連接池,spring也可以很好的管理它.
基于xml格式的配置元數據,可以如下指定bean的class
~~~xml
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
~~~
需要給構造方法提供參數,或者在對象實例化后設置屬性,參考[依賴注入](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#beans-factory-collaborators)
## Instantiation with a static factory method
當定義的bean由靜態工廠方法創建時,使用`class`指定包含靜態工廠方法的類(不是工廠方法返回對象的類),屬性`factory-method`指定工廠方法.你可以調用這個方法返回一個活動的對象,隨后把它當做是通過構造方法創建的.這種bean定義方式的用途是調用歷史代碼的靜態工廠方法.
下面的bean定義,指定bean是由工廠方法創建的.僅指定了含有工廠方法的類,并沒有指定返回對象的類型.在這個例子中,`createInstance()`必須是靜態方法.
~~~xml
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
~~~
~~~java
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
~~~
## Instantiation using an instance factory method
類似靜態工廠方法實例化,實例工廠方法實例化就是調用容器中已存在的bean的非靜態方法來創建bean.使用這種機制,`class`屬性為空,使用`factory-bean`屬性指定容器中bean的name,這個bean包含創建對象的實例方法,`factory-method`指定工廠方法.
~~~xml
<!-- 工廠類, 包含方法 createClientServiceInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
</bean>
<!-- 工廠類創建的bean -->
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>
~~~
~~~java
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
}
~~~
一個工廠類可以包含多個工廠方法:
~~~xml
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
</bean>
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>
<bean id="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/>
~~~
~~~java
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
public AccountService createAccountServiceInstance() {
return accountService;
}
}
~~~
- 正確打開本書的姿勢
- 第一部分 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