3.3 概述
Table 3.1. The bean definition
Property | Explained in... |
---|---|
class | |
name | |
scope | |
constructor arguments | |
properties | |
autowiring mode | |
lazy-initialization mode | |
initialization method | |
destruction method |
3.3.1 命名beans
一个bean通常只有一个标示符(id),但是可以拥有多个别名(name)。可以通过使用“,”、“;”、“ ”来为name定义多个别名。
bean不一定要有一个name或者一个id,如果都没有配置的话,spring默认为这个bean生成一个唯一的名字。但是如果要通过ref属性或Service Location来引用的话,必须提供一个名字。什么都不配置的情况通常是用在inner beans和autowiring collaborators。
3.3.1.1 bean定义之外的别名
通过<alias/>标签可以为同一个bean定义不同的别名
<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/> <alias name="subsystemA-dataSource" alias="myApp-dataSource" />3.3.2 实例化beans
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>
public class ClientService { private static ClientService clientService = new ClientService(); private ClientService() {} public static ClientService createInstance() { return clientService; } }3.3.2.3 通过实例化一个工厂方法来初始化
<!-- the factory bean, which contains a method called createInstance() --> <bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --> </bean> <!-- the bean to be created via the factory bean --> <bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() { return clientService; } }
Spring3.0官网文档学习笔记(五)--3.3,布布扣,bubuko.com
原文地址:http://blog.csdn.net/abauch_d/article/details/32926775