一 两种方式
bean工厂:最简单的容器,提供了基础的依赖注入支持。创建各种类型的Bean。
应用上下文(ApplicationContext):建立在bean工厂基础之上,提供系统架构服务。
二 实例
1 Student
package com.hsp.ioc;
//java类都有一个默认的无参构造函数
public class Student {
String name;
public Student(){
System.out.println("student被创建");
}
public String getName() {
return name;
}
public void setName(String name) {
//this.name = name;
System.out.println("输入值"+name);
}
}
2 beans.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:tx="http://www.springframework.org/schema/tx";
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";>
<!-- 配置bean -->
<bean id="student" scope="singleton" class="com.hsp.ioc.Student">
<property name="name" value="小猪" />
</bean>
</beans>
3 App1
package com.hsp.ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class App1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//从ApplicationContext中取bean
//ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");
//当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是 singleton)
//从bean中取出student
//通过文件路径来获取
ApplicationContext ac=new FileSystemXmlApplicationContext("src\\com\\hsp\\ioc\\beans.xml");
//获取两个student
Student s1=(Student) ac.getBean("student");
Student s2=(Student) ac.getBean("student");
System.out.println(s1+" "+s2);
//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么
//容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
// BeanFactory factory = new XmlBeanFactory(
// new ClassPathResource("com/hsp/ioc/beans.xml"));
// factory.getBean("student");
}
}
4 测试结果
student被创建
输入值小猪
com.hsp.ioc.Student@7e694d91 com.hsp.ioc.Student@7e694d91
三 结论
1.如果使用ApplicationContext,则配置的bean如果是singlton,不管你用不用,都被实例化。(好处就是可以预先加载,缺点就是耗内存)。
2.如果是BeanFactory,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度慢)。
3.规定:一般没有特殊要求,应当使用ApplicatioContext完成(90%)。在很少的情况下,使用BeanFactory,如在移动设备。
4.ApplicationCotext是spring更加高级的容器,功能强大:
4.1.提供文本信息解析工具,包括对国际化支持。
4.2.提供载入文件资源的通用方法,如图片。
4.3.可以向注册为监听器的bean发送事件。
5.除了应用上下文提供的附加功能外,应用上下文与bean工厂的另一个重要区别是关于单例bean如何被加载。
bean工厂延迟加载所有bean,直到getBean()方法被调用。
应用上下文会(applicationContext)在启动后预载入所有单例bean,这样可确保应用不需要等待他们被创建。
四 三种经常用配置文件加载方式以及举例
1.ClassPathXmlApplicationContext:从类路径中加载。
2.FileSystemXmlApplicationContext:从文件系统加载。
3.XmlWebApplicationContext:从web系统中加载。
ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");
ApplicationContext ac=new FileSystemXmlApplicationContext("src\\com\\hsp\\ioc\\beans.xml");
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/hsp/ioc/beans.xml"));
五 bean的scope细节
request、session、global-session是在web开发中才有意义。
//获取两个student
Student s1=(Student) ac.getBean("student");
Student s2=(Student) ac.getBean("student");
System.out.println(s1+" "+s2);