Spring 基础
一、认识Bean
在Spring中,那些组成应用的主体(backbone)及由Spring IoC容器所管理的对象被称之为bean。简单地讲,bean就是由Spring容器初始化、装配及被管理的对象,除此之外,bean就没有特别之处了(与应用中的其他对象没有什么区别)。而bean定义以及bean相互间的依赖关系将通过配置元数据来描述。
二、认识BeanFactory
org.springframework.beans.factory.BeanFactory是Spring IoC容器的实际代表者,IoC容器负责容纳此前所描述的bean,并对bean进行管理。
BeanFactory负责读取Bean定义文件,管理对象的加载、生成、维护Bean对象与Bean对象之间的依赖关系,负责Bean的生命周期,对于简单的应用程序来说,使用BeanFactory就足够来管理Bean,在对象的管理上就可以获得许多的便利性。
BeanFactory是整个Spring围绕的重点。它负责读取Bean配置管理。可以借由getBean()方法来获取Bean的实例。
三、ApplicationContext
不过作为一个应用程序框架,只提供Bean容器管理的功能是不够的,若要利用Spring所提供的一些特色以及高级的容器功能,则可以使用BeanFactory的子接口ApplicationContext,此接口的基本功能与BeanFactory接口很相似,另外还提供了一个应用程序所需的更完整的框架功能:
1、提供获取资源文件的更方便的方法;
2、提供文字消息解析的方法;
3、支持国际化消息;
4、ApplicationContext可以发布时间,对时间感兴趣的Bean可以接收到这些事件。
简而言之,BeanFactory提供了配制框架及基本功能,而ApplicationContext则增加了更多支持企业核心内容的功能。ApplicationContext完全由BeanFactory扩展而来,因而BeanFactory所具备的能力和行为也适ApplicationContext
Spring的创始者Rod Johnson建议使用ApplicationContext来取代BeanFactory,在实现ApplicationContext的类中,最常使用的大概是一下三个:
org.springframework.context.support.ClassPathXmlApplicationContext
org.springframework.context.support.FileSystemXmlApplicationContext
org.springframework.web.context.support.XmlWebApplicationContext
四、接口选择之惑
在实际应用中,用户有时候不知道到底是选择BeanFactory接口还是ApplicationContext接口。但是通常在构建J2EE应用时,使用ApplicationContext将是更好的选择,因为它不仅提供了BeanFactory的所有特性,同时也允许使用更多的声明方式来得到我们想要的功能。
五、Spring的Bean配置
Spring支持三种配置元数据格式:XML格式、Java属性文件格式或使用Spring公共API编程实现。
六、实例化Spring Ioc容器
Resource resource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
ClassPathResource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) context;
七、Spring Demo
注入一个Bean,通过Bean上下文工具获取Bean对象并输出其中的属性。
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-17 16:53:54
* Note: 测试Bean
*/
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-17 16:53:54
* Note: 测试Bean
*/
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com.lavasoft.springnote.ch01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-18 15:00:46
* Note: Spring上下文工具
*/
public class BeanContextHelper {
private static ApplicationContext applicationContext;
static {
applicationContext = buildApplicationContext();
}
private BeanContextHelper() {
}
/**
* 重新构建ApplicationContext对象
* @return ApplicationContext
*/
public static ApplicationContext buildApplicationContext() {
return new ClassPathXmlApplicationContext( "applicationContext.xml");
}
/**
* 获取一个ApplicationContext对象
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-18 15:00:46
* Note: Spring上下文工具
*/
public class BeanContextHelper {
private static ApplicationContext applicationContext;
static {
applicationContext = buildApplicationContext();
}
private BeanContextHelper() {
}
/**
* 重新构建ApplicationContext对象
* @return ApplicationContext
*/
public static ApplicationContext buildApplicationContext() {
return new ClassPathXmlApplicationContext( "applicationContext.xml");
}
/**
* 获取一个ApplicationContext对象
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
xml
version
="1.0"
encoding
="UTF-8"
?>
< beans >
< bean id ="person" class ="com.lavasoft.springnote.ch01.Person" >
< property name ="name" >
< value >lavasoft value >
property >
< property name ="age" >
< value >22 value >
property >
bean >
beans >
< beans >
< bean id ="person" class ="com.lavasoft.springnote.ch01.Person" >
< property name ="name" >
< value >lavasoft value >
property >
< property name ="age" >
< value >22 value >
property >
bean >
beans >
package com.lavasoft.springnote.ch01;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-17 16:58:54
* Note: BeanFactory获取工具(测试版)
*/
public class SpringUtils {
private static SpringUtils _instance = new SpringUtils();
private SpringUtils() {
}
public static ClassPathXmlApplicationContext getClassPathXmlApplicationContext() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
return context;
}
public static BeanFactory getBeanFactory() {
Resource rs = new FileSystemResource( "applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(rs);
return factory;
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-17 16:58:54
* Note: BeanFactory获取工具(测试版)
*/
public class SpringUtils {
private static SpringUtils _instance = new SpringUtils();
private SpringUtils() {
}
public static ClassPathXmlApplicationContext getClassPathXmlApplicationContext() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
return context;
}
public static BeanFactory getBeanFactory() {
Resource rs = new FileSystemResource( "applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(rs);
return factory;
}
}
package com.lavasoft.springnote.ch01;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-17 16:58:07
* Note: 客户端测试
*/
public class Test {
public static void main(String args[]) {
Test test = new Test();
test.test3();
// test.test2();
}
public void test1() {
//bean的配置文件“/applicationContext.xml”必须位于CLASSPATH下
BeanFactory factory = SpringUtils.getBeanFactory();
Person person = (Person) factory.getBean( "person");
System.out.println(person.getName());
System.out.println(person.getAge());
}
public void test2(){
//bean的配置文件“/applicationContext.xml”必须位于源码src下
ApplicationContext context = SpringUtils.getClassPathXmlApplicationContext();
Person person = (Person) context.getBean( "person");
System.out.println(person.getName());
System.out.println(person.getAge());
}
public void test3(){
ApplicationContext context = BeanContextHelper.getApplicationContext();
Person person = (Person) context.getBean( "person");
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-17 16:58:07
* Note: 客户端测试
*/
public class Test {
public static void main(String args[]) {
Test test = new Test();
test.test3();
// test.test2();
}
public void test1() {
//bean的配置文件“/applicationContext.xml”必须位于CLASSPATH下
BeanFactory factory = SpringUtils.getBeanFactory();
Person person = (Person) factory.getBean( "person");
System.out.println(person.getName());
System.out.println(person.getAge());
}
public void test2(){
//bean的配置文件“/applicationContext.xml”必须位于源码src下
ApplicationContext context = SpringUtils.getClassPathXmlApplicationContext();
Person person = (Person) context.getBean( "person");
System.out.println(person.getName());
System.out.println(person.getAge());
}
public void test3(){
ApplicationContext context = BeanContextHelper.getApplicationContext();
Person person = (Person) context.getBean( "person");
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
运行结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
lavasoft
22
Process finished with exit code 0
log4j:WARN Please initialize the log4j system properly.
lavasoft
22
Process finished with exit code 0