一 IOC 容器(Inversion of Control)
1. IOC容器和beans的介绍 (Introduction to the Spring IoC Container and Beans)
org.springframework.bean和 org.springframework.context是 Spring的容器的基础jar包;
BeanFactory接口提供能够管理任何类的高级机制;
ApplicationContext是 BeanFactory 的一个子接口,它增加以下功能:
* 更容易的和 Spring‘s AOP 融合
* 消息资源处理
* 事件发布
* 应用层特定的上下文 例如 WebApplication
2.容器概述
spring容器通过读取配置i资源加载bean,判断如何实例化bean和装配bean,
这些配置资源可以是XML 注解也可以是java代码。
2.1 配置元数据
- 使用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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
2.使用注解
@Bean @Import @Configuration @DependsOn
3. 使用java代码
在一个使用了@Cofiguration注解了的类的方法上使用@Bean注解。
2.2 实例化一个容器
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
2.3 使用容器
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
3 Bean概述
在spring容器中bean是根据我们提供的配置元数据创建的,在spring容器内ben的定义都存储在BeanDefinition对象中,它通常包含以下元素:
- 包限定的类名
- bean的一些行为配置,例如:范围 生命周期 回调等
- bean的一些依赖关系
- 要在新创建的对象中设置的其他配置设置—例如,池的大小限制或要在管理连接池的bean中使用的连接数。
3.1 bean的定义
3.2 bean的实例化
- 通过构造函数实例化一个bena
直接在xml文件配置就可以
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
- 通过一个静态工厂方法实例化一个bean
<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;
}
}
- 通过实例工厂实例化一个bean
<!-- the factory bean, which contains a method called createClientServiceInstance() -->
<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();
public ClientService createClientServiceInstance() {
return clientService;
}
}