第2章.IoC容器
IoC容器概述
abstract: 介绍IoC和bean的用处和使用
IoC容器处于整个Spring框架中比较核心的位置:Core Container: Beans, Core, Context, SpEL
为上层AOP/Aspects/Instrumentation/Messaging提供支持
IoC容器的用途:
创建对象的依赖,最后组装成所需的业务对象
容器通过业务对象和配置(application-context.xml; xxxController.java)-->生成可用对象
在Spring场景下,把对象A/B/C都称作Bean
从代码角度看,一个ApplicationContext (org.springframework.context包下,属于spring-context依赖中)对象就是一个IoC容器
初始化IoC容器:
在web项目的web.xml中,用
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
(文件系统中:ApplicationContext context = new FileSystemXmlApplicationContext("/home/user/conf/application-context.xml");)
来初始化。
定义一个Bean:
在application-context.xml中
<beans>
...
<bean id="screwDriver" class="package_url"></bean>
</beans>
使用Bean:
1. 初始化容器:ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
2. 通过getBean()获取对象:ScrewDriver screwDriver = context.getBean("screwDriver", ScrewDriver.class);
3. 使用对象:screwDriver.use();
实例演练:
1. New Maven Project -> Create a simple project (skip archetype selection)
GroupId: com.netease.nanodegree
ArtifactId: spring-container
2. 将所需依赖加入pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.1.RELEASE</version> </dependency> </dependencies>
发现依赖的jar包自动被加入
3. 在src/main/resources/下新建application-context.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
此时,还未定义任何bean,因此还不能从容器中获取任何bean
4. 在src/main/java/下新建类ScrewDriver.java
package: com.netease.nanodegree;
package com.netease.nanodegree; public class ScrewDriver { public void ues () { System.out.println("Use screw driver"); } }
5. 此时有了bean,在application-context.xml中加入相关bean的定义
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #3933ff }
span.s1 { color: #009193 }
span.s2 { color: #4e9192 }
span.s3 { color: #000000 }
span.s4 { color: #932192 }
<bean id = "screwDriver" class = "com.netease.nanodegree.ScrewDriver"></bean>
6. 在src/main/java/com.netease.nanodegree下新建测试类TestContainer.java
7. Run as -> Java Application
Bean的作用域:Bean的有效范围
singleton:与设计模式中的singleton有点像
保证对象在整个容器中最多只能有一个单例,也保证在整个生命周期中只会创建一个实例
上例中bean定义的默认作用域就是singleton
也可以显式定义<bean id="screwDriver" class="com.netease.nanodegree.ScrewDriver" scope="singleton"></bean>
实例Test:
ScrewDriver.java
public class ScrewDriver { private String color = "red"; public void use () { //System.out.println("Use screw driver"); System.out.println("Use " + color + " screw driver"); } public void setColor (String color) { this.color = color; } }
TestContainer.java
要想测试是不是单例对象,使用getBean()得到两个对象并设置属性为"green",如果是单例,则两个对象的属性均为"green"
prototype
在web应用场景下才出现的:
request
global session
session
application