Spring容器
启动Spring容器(实例化容器)
Spring容器
启动Spring容器(实例化容器)
IOC容器读取Bean配置创建Bean实例之前,必须先对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用。Bean是Spring管理的基本单位,任何的Java对象和组件都被当成Bean处理,容器还负责管理Bean与Bean之间的依赖关系。
两种类型的启动实现
1、BeanFactory:IOC容器的基本实现,是Spring框架的基础设施,面向Spring本身使用:
Spring容器最基本的接口就是BeanFactor。BeanFactory负责配置、创建、管理Bean。
BeanFactory接口包含以下几个基本方法:Boolean containBean(String name):判断Spring容器是否包含id为name的Bean实例。
<T> getBean(Class<T> requiredTypr):获取Spring容器中属于requiredType类型的唯一的Bean实例。
Object getBean(String name):返回Spring容器中id为name的Bean实例。
<T> T getBean(String name,Class requiredType):返回容器中id为name,并且类型为requiredType的Bean实例
Class <?> getType(String name):返回容器中指定Bean实例的类型。
其他的方法与属性可通过如下方式查看:
在Eclipse开发工具中,选中BeanFactory后按F4或者快捷键Ctrl+T 快速显示当前类(接口)的继承结构
2、ApplicationContext:面向使用Spring框架的开发者,几乎所有的应用都使用它而非底层的BeanFactory:
ApplicationContext是BeanFactory的子接口,一般都会用ApplicationContext,在使用上相比BeanFactory更方便一些
(拓展了一些常用方法)。
查看下 ApplicationContext的继承结构:
看结构能够看出ApplicationContext有1个子接口,多个抽象类,2个实现类
- 一个子接口:ConfigurableApplicationContext
此接口新增两个主要方法refresh和close,让ApplicationContext具有启动、刷新和关闭上下文的能力
2. 两个实现类:ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
① ClassPathXmlApplicationContext : 从类路径下加载配置文件
文件路径:默认指的是项目的classpath路径下面,所以不需要写前缀classpath:。如果指向绝对路径,需要加上file:
② FileSystemXmlApplicationContext:从文件系统中加载配置文件
文件路径:默认指的是项目的根目录下,想使用项目的classpath路径下面,需要加上classpath:。
案例演示目录结构:
采用Maven构建项目:
导入Spring所需Jar依赖节点配置:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.frame</groupId>
<artifactId>Spring-StartObject-Analysis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
创建JavaBean对象:HelloWorld.java
package com.dk.spring.bean;
public class HelloWorld {
private String name;
public void setName(String name) {
System.out.println("调用了设置属性");
this.name = name;
}
public HelloWorld() {
System.out.println("初始化构造器");
}
public void hello() {
System.out.println("Hello: " + name);
}
}
创建Spring的配置文件配置对应JavaBean:
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="helloworld" class="com.dk.spring.bean.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
</beans>
最后创建Spring容器启动文件:SpringContainer.java
package com.dk.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.dk.spring.bean.HelloWorld;
public class SpringContainer {
public static void main(String[] args) {
//BeanFactory
// 1、创建Spring的IOC容器的对象
ApplicationContext ctx0 = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext ctx1 = new ClassPathXmlApplicationContext("file:filepath/applicationContext.xml");
ApplicationContext ctx2 = new FileSystemXmlApplicationContext("filepath/applicationContext.xml");
ApplicationContext ctx3 = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
// 2、从IOC的容器中获取Bean的实例
HelloWorld helloWorld = (HelloWorld) ctx2.getBean("helloworld");
// 3、调用hello方法
helloWorld.hello();
}
}
运行SpringContainer 的main方法测试不同方式加载配置文件的方式:
控制台输出信息如下:
我们发现两种方法都是可以的,不过一般我们使用类路径相对路径加载配置文件实例化Spring容器。
ApplicationContext ctx0 = new ClassPathXmlApplicationContext("applicationContext.xml")