本帖内容简介
本帖以一个实际存在的类为基础,介绍如何启动springIoc及直观形象的通过main函数从xml文件中获取到指定的Bean。
BeanFactory简介:
BeanFactory(com.springframework.beans.factory.BeanFactory)是spring框架最核心的接口,提供了高级IOC的配置机制。使用BeanFactory使管理不同类型的java对象成为可能,是spring框架的基础设施。一般来讲,我们称BeanFactory为Ioc容器。
前期准备:
1. 用户在某个package下(例如com.bbt.car)定义了一个叫做Car的类,其中包含了label, speed, color三个元素,以及它们的set和get方法。
public class Car
{
private String label;
private int speed;
private int color;
public Car()
{
super();
}
public Car(String label, int speed, int color) {
super();
this.label = label;
this.speed = speed;
this.color = color;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
@Override
public String toString() {
return " [label=" + label + ", speed=" + speed + ", color=" + color + "]";
}
}
2. 用户同样在该package下(com.bbt.car)定义了一个叫做conf.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="temp" class="com.baobaotao.car.Car"
p:speed="900"
p:color="10"
p:label="2"
/>
<!-- <bean id="car" class="com.baobaotao.car.Car"
init-method="init"
p:speed="200"
/>-->
</beans>
三 使用BeanFactory从xml文件中获取到指定的Bean
在任意目录下定义了一个包含有main函数的类,可以使用XmlBeanFactory从指定的xml文件中获取到指定的bean,见下面代码:
public static void main(String []args)throws Throwable
{
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try
{
Resource res = resolver.getResource("classpath:com/bbt/car/conf.xml");
BeanFactory bf = new XmlBeanFactory(res);//生成BeanFactory,此时并没有将conf.xml中的bean放置到容器中。
Car car01 = (Car) bf.getBean("temp");//从conf.xml中获取到Id为temp的bean,此时才开始装载。
System.out.println("speed : " + car01.getSpeed());
System.out.println("color : " + car01.getColor());
System.out.println("Label: " + car01.getLabel());
}
catch(Exception e)
{
System.out.println("Exception: "+String.valueOf(e));
}
}
四 使用ApplicationContext获取Bean
ApplicationContext提供了两种哦那个从xml中获取到Bean的方法,即 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext。 两个的区别在于所获取xml的路径不同,前者从类路径下获取xml配置文件的内容,而后者则是从文件路径中获取到具体xml的位置,下面以例子分别进行说明:
public static void main(String []args) throws Throwable
{
try
{
//方法1:类路径
ApplicationContext act = new ClassPathXmlApplicationContext("com/bbt/car/conf.xml");
Car car = (Car) act.getBean("temp");
//方法2:文件路径,在\build\classes下
String file = new ApplicationContextTest().getPath()+"conf.xml";
act = new FileSystemXmlApplicationContext(file);
System.out.println(car);
//System.out.println(new ApplicationContextTest().getPath());
}
catch(Exception e)
{
System.out.println(String.valueOf(e));
}
}
String getPath()
{
return this.getClass().getClassLoader().getResource("").getPath();
}
1. ClassPathXmlApplicationContext方式
上图中方法1从制定的包中获取到xml文件。
2.FileSystemXmlApplicationContext方式
file路径一般是指在磁盘中的路径,实际应用中一般在build\classes之下。
五 注意点
1. spring Ioc启动的时间:如注释中所说,Ioc并不是在创建BeanFactory的时候将内容存储,而是在第一次调用的时候才会将内容读取。
2. ApplicationContext则是在初始化的时候就已经将内容写入到容器中,在实际应用中比较直接方便。
2. 使用过程中必须有相应的日志文件,如comm-logging或者log4j 等。