Spring总结_04_容器和bean
一.概念理解
1.容器
IoC容器负责容纳并管理bean,在Spring中,BeanFactory是IoC容器的核心接口。 它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。
Spring提供两种IoC容器的实现:
(1)BeanFactory :org.springframework.beans.factory.BeanFactory 是Spring IoC容器的基本实现,是Spring IoC容器实际代表者
BeanFactory是Spring框架的基础设施,面向Spring本身
(2)ApplicationContext:提供了更多的高级特性,是BeanFactory的子类。
ApplicationContext面向开发者。几乎所有的场合都直接使用ApplicationContext而非底层的BeanFactory。

ApplicationContext 的主要实现类:
ClassPathXmlApplicationContext:从 类路径下加载配置文件
FileSystemXmlApplicationContext: 从文件系统中加载配置文件
ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力
2.bean
在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象
bean的定义由BeanDefinition 对象来表示,该定义将包含以下信息:
- 全限定类名: 这通常就是已定义bean的实际实现类。
- bean行为的定义: 这些定义将决定bean在容器中的行为(作用域、生命周期回调等等)
- 对其他bean的引用: 这些引用bean也可以称之为协作bean(collaborators) 或依赖bean(dependencies).
- 创建bean实例时的其他配置设置。比如使用bean来定义连接池,可以通过属性或者构 造参数指定连接数,以及连接池大小限制等。
上述内容直接被翻译为每个bean定义包含的一组properties。
二.bean的配置
1. 简单实例
java bean: HelloWorld.java
package com.ray.spring;
public class HelloWorld {
private String name;
public void setName(String name){
this.name=name;
}
public void hello(){
System.out.println("hello:"+name);
}
}
配置文件:applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置一个 bean
class:bean的全类名,通过反射的方式在IoC容器中穿件Bean.所以要求Bean中必须有无参构造器
id:标识容器中的bean,id唯一。若有多个id,则第一个是标识符,其他为别名。
-->
<bean id="helloWorld" class="com.ray.spring.HelloWorld">
<!-- 为属性赋值
通过属性注入: 通过 setter 方法注入属性值
-->
<property name="name" value="Tom"></property>
</bean>
</beans>
bean的实例化:Main.java
package com.ray.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1. 创建 Spring 的 IOC 容器,ApplicationContext代表IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
/*2. 从 IOC 容器中获取 bean 的实例
2.1利用id定位到IoC容器中的bean
*/
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//2.2利用类型返回IoC容器中的bean,但要求IoC容器中必须只有一个该类型的bean
//HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3. 使用 bean
helloWorld.hello();
}
}
2. bean的依赖注入
Spring 支持 3 种依赖注入的方式:属性注入、构造器注入、工厂方法注入(很少使用,不推荐)
2.1 属性注入
属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
属性注入是实际应用中最常用的注入方式。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="helloWorld" class="com.ray.spring.HelloWorld">
<!-- 通过属性注入: 通过 setter 方法注入属性值
<property>节点:设置属性
name :指定 Bean 的属性名称
value 或 <value> 子节点:指定属性值
-->
<property name="name" value="Tom"></property>
</bean>
</beans>
2.2 构造器注入
构造器注入即 通过配置构造器参数,来注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
Java bean: Car.java
package com.ray.spring;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", maxSpeed=" + maxSpeed + "]";
}
}
配置文件: applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 通过构造器注入属性值
1.按参数顺序注入-->
<bean id="car1" class="com.ray.spring.Car">
<!-- 都要求: 在 Bean 中必须有对应的构造器. -->
<constructor-arg value="Audi"></constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean>
<!--2.通过参数索引注入-->
<bean id="car2" class="com.ray.spring.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
<!--3.通过参数类型注入-->
<bean id="car3" class="com.ray.spring.Car">
<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="300000" type="int"></constructor-arg>
</bean>
</beans>
bean的实例化:
package com.ray.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1. 创建 Spring 的 IOC 容器,ApplicationContext代表IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2. 从 IOC 容器中获取 bean 的实例
Car car=(Car)ctx.getBean("car1");
System.out.println(car);
car=(Car)ctx.getBean("car2");
System.out.println(car);
car=(Car)ctx.getBean("car3");
System.out.println(car);
}
}
Spring总结_04_容器和bean的更多相关文章
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- Spring IOC 一——容器装配Bean的简单使用
下文:SpringIOC 二-- 容器 和 Bean的深入理解 写在前面 这篇文章去年写的,缘起于去年某段时间被领导临时"抓壮丁"般的叫过去做java开发,然后在网上找了一个 Sp ...
- Spring重点—— IOC 容器中 Bean 的生命周期
一.理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助. 二.在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理. 三.在没有添加后置处理器的情况 ...
- Spring为IOC容器注入Bean的五种方式
一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...
- Spring技术内幕_IOC容器载入Bean定义资源文件
转自:http://blog.csdn.net/chjttony/article/details/6259723 1.当spring的IoC容器将Bean定义的资源文件封装为Spring的Resour ...
- SpringIOC 二—— 容器 和 Bean的深入理解
上文:Spring IOC 一--容器装配Bean的简单使用 上篇文章介绍了 Spring IOC 中最重要的两个概念--容器和Bean,以及如何使用 Spring 容器装配Bean.本文接着记录 S ...
- spring容器对bean生命周期的管理三中方式
spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)方式一:使用springXML配置中的init-meth ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring基础——在 IOC 容器中 Bean 之间的关系
一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...
随机推荐
- C标准I/O建立一个文件仓库
实现了 增删改查 创建了一个结构体 保存 num name cout price 代码 #include<stdio.h>#include<string.h>#include& ...
- 超炫的时间轴jquery插件Timeline Portfolio
Timeline Portfolio是一款按时间顺序专业显示事件的jquery时间轴插件,可以根据时间的先后嵌入各种媒体包括微博,视频和地图等.这个展现的模式非常适合设计师的作品集和个人简历的展示.T ...
- Android中的AutoCompleteTextView的使用
最终的效果如下: main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- doubango(2)--底层协议栈结构分析
tsip_stack_handle_t 实例 1. tsip_stack_handle_t的创建 在底层,真正运转的协议栈结构式tsip_stack_handle_t的一个实例,它的创建 ...
- Java中的集合类型的继承关系图
Java中的集合类型的继承关系图
- Linux 服务器设置成支持中文
Linux 服务器设置成支持中文 由于服务器默认是不支持中文的.所以一般需要单独设置一下. 检查本机已有的语言包 locale -a 默认是没有中文的,所以会显示: C C.UTF-8 POSIX e ...
- 理解Window和WindowManager
Window表示一个窗口的概念,Window是一个抽象类,它的具体实现是PhoneWindow.创建一个Window,需要通过WindowManager即可完成,WindowManager是外界访问W ...
- [工作总结] QA小鸟一年了
夏至又至,在北京360的一年过去了.作为一名QA,我时常感到迷惑,如何靠大部分的手工测试+少部分的自动化测试来保证产品功能的质量.对于开发完成后,给到我手上的文件和功能说明,我很少有信心说能够通过自己 ...
- redis&rabbitMQ安装
前言: 学习python已经有一段时间了,最近在学twisted(博客:twisted安装),redis,rabbitMQ感觉有点难度,所以还是写下博客整理下. 一.Redis的安装 redis是一种 ...
- oracle 11G RAC会话故障转移测试
目前接手的几个项目中,默认使用的oracle RAC数据库服务,均不能实现自动的会话转移,尤其是对于应用的长连接,一旦发生数据库故障,需要重启应用.实际11G具备会话迁移机制,为此做了如下配置测试,供 ...