Spring核心技术之容器和Bean Mr

在spring中,那些组成应用程序主体 及springIOC容器所管理的对象,被称之为 bean,简单的讲,bean就是由spring容器初始化,装配及管理的对象,除此之外,bean就与应用程序中的其他对象没有什么区别了。而bean定位以及bean相互间的依赖关系将通过配置元数据来进行描述。

容器

org.springframework.beans.factory.BeanFactory 是Spring IoC容器的实际代表者,IoC容器负责容纳此前所描述的bean,并对bean进行管理。

在Spring中,BeanFactory是IoC容器的核心接口。 它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。

Spring为我们提供了许多易用的BeanFactory实现, XmlBeanFactory就是最常用的一个。该实现将以XML方式描述组成应用的对象 以及对象间的依赖关系。XmlBeanFactory类将获取此XML配 置元数据,并用它来构建一个完全可配置的系统或应用。

 

上图形象的说明了spring容器通过配置的元数据及普通的java类产生备用的应用。

下边基于springmvc中的项目进行简单的介绍:

此功能的依赖jar包:spring.jar

依赖类:

ApplicationContext.class

ClassPathXmlApplicationContext.class

简单代码实现分层理解spring的IOC概念:

package springapp.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 简单测试类  
 * @author zhangxuegang       
 * @version 1.0     
 * @created 2012-10-15 下午01:47:18
 */
  
public class TestSpring {  
    public static void main(String[] args) throws Exception {  
        StringBuffer buffer=new StringBuffer();  
           ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                  "file:F:/springProject/springapp/war/WEB-INF/applicationTest.xml");
           TestSpring tp = (TestSpring) ctx.getBean("testSpring");
           tp.test();
        }  
    public void test(){
        System.out.println("测试成功");
    }
      
}  

applicationTest.xml对刚才的对象进行配置,在spring中描述为bean

<?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: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-2.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     <bean id="testSpring" class="springapp.test.TestSpring">
    </bean>
</beans>

Xml对bean进行了详细的依赖描述:

而实例化bean有很多方法,上边的方法是spring中所说的构造器方法实例化

<bean id="testSpring" class="springapp.test.TestSpring">

其中还有使用静态工厂实例化:

<bean id="testSpring" class="springapp.test.TestSpring"

factory-method=”createInstance”>

使用实例工厂方法实例化:

<!-- the factory bean, which contains a method called createInstance() -->

<bean id="serviceLocator" class="com.foo.DefaultServiceLocator">

  <!-- inject any dependencies required by this locator bean --></bean>

<!-- the bean to be created via the factory bean --><bean id="exampleBean"

      factory-bean="serviceLocator"      factory-method="createInstance"/>

现在我们知道,spring通过BeanFactory和配置文件维护bean定义及bean之间的相互依赖关系,那我们怎样用这些bean呢,从本质上讲,BeanFactory仅仅只是一个 维护bean定义以及相互依赖关系的高级工厂接口。通过BeanFactory 我们可以访问bean定义。下面的例子创建了一个bean工厂,此工厂 将从xml文件中读取bean定义:

Resource res = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(res);

基本上就这些了,接着使用getBean(String)方法就可以取得bean的实例;BeanFactory提供的方法极其简单。 BeanFactory接口提供 了非常多的方法,但是对于我们的应用来说,最好永远不要调用它们,当然也包括 使用getBean(String)方法,这样可以避免我们对 Spring API的依赖。

Mr-sniper
北京市海淀区
邮箱:rafx_z@hotmail.com
原文地址:https://www.cnblogs.com/rafx/p/containerAndBean.html