02 spring 核心技术-IOC容器(概述)

一 IOC 容器(Inversion of Control)

1. IOC容器和beans的介绍 (Introduction to the Spring IoC Container and Beans)

org.springframework.bean和 org.springframework.context是 Spring的容器的基础jar包;
BeanFactory接口提供能够管理任何类的高级机制;
ApplicationContextBeanFactory 的一个子接口,它增加以下功能:
* 更容易的和 Spring‘s AOP 融合
* 消息资源处理
* 事件发布
* 应用层特定的上下文 例如 WebApplication

2.容器概述

在这里插入图片描述
spring容器通过读取配置i资源加载bean,判断如何实例化bean和装配bean,
这些配置资源可以是XML 注解也可以是java代码。

2.1 配置元数据

  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;
	}
}
  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值