`
zqj15011
  • 浏览: 7974 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring组件

 
阅读更多

 

Spring 组件

      Spring3.0以后的版本和以前的版本开发包组织方式不同了,官方也不将其依赖包一并包含在其中,要想使用,就必须需要下载相应的依赖包:


 Spring核心就是以Bean为中心构建的一个Bean关系网,对Bean进行解析,创建,通过Bean将其它框架整合进来,进而以Bean的方式来访问。

 诚如上图所示,用红线画出的就是Spring的核心组件,即: Core、Context、beans 。而其余特性jms、orm、aop等都是以该三个核心组件为基础实现扩展的。

三大核心组件的协同工作主要表现在 :Bean是包装我们应用程序自定义对象Object的,Object中存有数据,而Context就是为了这些数据存放提供一个生存环境,保存各个bean之间的对应关系,并且维护好这些对应关系。Context就是一个Bean关系的集合,也就是我们所谓的IOC容器。Core就是Context在发现、建立、维护Bean之间关系所需要的一些工具,如资源的加载,资源的抽象等。

  1. Bean组件
    这个包下的所有类主要解决了三件事:Bean的定义、Bean 的创建以及对Bean的解析。对Spring的使用者来说唯一需要关心的就是Bean的创建,其他两个由Spring在内部帮你完成了,对你来说是透明的。
    BeanFactory有三个子类:ListableBeanFactory、HierarchicalBeanFactory和Autowire Capable Bean Factory。但是最终的默认实现类是DefaultListableBeanFactory,他实 现了所有的接口。那为何要定义这么多层次的接口呢?查阅这些接口的源码和说明发现,每个接口都有他使用的场合,它主要是为了区分在Spring内部在操作过程中对象的传递和转化过程中,对对象的 数据访问所做的限制。例如ListableBeanFactory接口表示这些Bean是可列表的,而HierarchicalBeanFactory表示的是这些Bean是有继承关系的,也就是每个Bean有可能有父Bean。 AutowireCapableBeanFactory接口定义Bean的自动装配规则。这四个接口共同定义了Bean的集合、Bean之间的关系、以及Bean行为。
  2. Context组件
     Context组件就是为Bean对象提供一个运行时环境,标识一个运行时环境,初始化BeanFactory,利用BeanFactory来将解析注册的Bean进行创建,保存Bean对象之间的关系。Context可以说是将Core和Bean两个组件融合在一起了。Context组件的根级组件是ApplicationContext,既继承了BeanFactory,同时又继承了Core中的Resource类。
  3.  Core组件
    Core组件主要就是定义了访问资源的方式,以及对于各种资源进行用统一的接口来抽象,屏蔽了具体资源的类型。资源的顶级接口为Resource,它继承自InputStreamResource,实现了其getInstream方法,这样所有的资源就是通过该方法来获取输入流的。对于资源的加载,也实现了统一,定义了一个资源加载顶级接口 ResourceLoader,它默认的加载就是DefaultResourceLoader。
Spring 对象生命周期
  1. singleton  
    当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton作用域是Spring中的缺省作用域,配置如下:

 

<bean id="accountService" class="com.foo.DefaultAccountService"/>  
<!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd  or upper-->  
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>  
  
<!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->  
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>  

 

测试代码如下所示:

public class SpringTest {  
  
@Test   
public void instanceSpring(){  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
       PersonService personService1 = (PersonService)ctx.getBean("accountService");  
       PersonService personService2 = (PersonService)ctx.getBean("accountService");  
        System.out.println(personService1==personService2);  
    }  
}  

 

     2. Prototype作用域

          Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean() 方法)时都会创建一个新的bean实例 。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:

<!-- using spring-beans-2.0.dtd or upper -->  
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>  
<!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->  
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>  

 

 

     3.其他作用域

      request、session以及global session 仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架)。这些作用域仅仅在使用基于web的Spring ApplicationContext实现(如XmlWebApplicationContext)时有用。 如果在普通的Spring IoC容器中,比如像XmlBeanFactory或ClassPathXmlApplicationContext, 尝试使用这些作用域,你将会得到一个IllegalStateException异常(未知的bean作用域)。

 

Bean的生命周期

 

(1)什么时候初始化bean实例

当scope=singleton,即默认情况,会在容器初始化时实例化。但我们可以指定Bean节点的lazy-init=”true”来延迟初始化bean,这时候,只有第一次获取bean才会初始化bean,即第一次请求该bean时才初始化。如下配置所示:

<bean id=”xxx” class=”examples.test.OrderServiceBean” lazy-init=”true” />  

 

 如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=”true”,如下所示: 

<beans default-lazy-init=”true” …>  

 

当scope=prototype时,也会延迟初始化bean,即第一次请求该bean时才初始化(如调用getBean()方法时).

测试如下:

PersonServiceBean代码如下所示:

public class PersonServiceBean implements PersonService {  
public void init(){  
        System.out.println("Get the database connection and initialize other beans!");  
    }  
    public PersonServiceBean(){  
        System.out.println("PersonServiceBean is initialized!");  
    }  
public void destroy(){  
        System.out.println("close the database connection!");  
    }  
}  

 

 beans.xml配置文件如下所示:

<bean id="personService" class="examples.test.PersonServiceBean" /> <!-- 1 -->  
<bean id="personService" class="examples.test.PersonServiceBean" scope="singleton" /> <!-- 2 -->  

 

测试程序如下所示:

 

public class SpringTest {  
@Test  
 public void instanceSpring(){  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
        System.out.println("------------------");  
        PersonService personService = (PersonServiceBean)ctx.getBean("personService");  
}  
} 

 收藏代码

 分1,2两种情况运行程序,控制台打印信息如下所示:

PersonServiceBean is initialized!
------------------

将配置文件修改成如下所示:

<bean id="personService" class="examples.test.PersonServiceBean"  lazy-init="true" /> <!-- 1 -->  
<bean id="personService" class="examples.test.PersonServiceBean" scope="prototype" /> <!-- 2 -->  

 

  分1,2两种情况运行程序,控制台打印信息如下所示:

------------------
PersonServiceBean is initialized!

  以上测试说明:

Bean默认是在容器初始化时初始化的,即

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");语句执行时就实例化bean了。如果把scope设成scope=”prototype” 或设置lazy-init=”true”,则会延迟bean的实例化,bean会在PersonService personService = (PersonServiceBean)ctx.getBean("personService");语句执行时才实例化。

当配置文件为如下所示时,测试结果同上。

<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/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"  
>  
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" >  
</beans>  

 

  (2)生命周期:

构造器、init方法、获取bean后的操作、destroy方法(ctx.close时执行).

注意:如果bean的scope设为prototype时,当ctx.close时,destroy方法不会被调用.

原因:对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法。但对prototype而言,任何配置好的析构生命周期回调方法都将不会 被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被prototype作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)谈及prototype作用域的bean时,在某些方面你可以将Spring容器的角色看作是Java new 操作的替代者。任何迟于该时间点的生命周期事宜都得交由客户端来处理。

配置文件信息如下:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destroy"/> 

 

  测试程序如下:

public class SpringTest {  
@Test   
public void instanceSpring(){  
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
        System.out.println("------------------");  
        PersonService personService = (PersonServiceBean)ctx.getBean("personService");  
        ctx.close();  
}  

 

运行测试程序,控制台打印信息如下所示:

PersonServiceBean is initialized!
Get the database connection and initialize other beans!
------------------
close the database connection!

修改配置文件信息如下:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype" init-method="init" destroy-method="destroy"/>  

 

再次运行测试程序,控制台打印信息如下所示:

------------------
PersonServiceBean is initialized!
Get the database connection and initialize other beans!

修改配置文件信息如下所示:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="true" init-method="init" destroy-method="destroy"/>  

 

此时再次运行测试程序,控制台打印信息如下所示:

 

------------------
PersonServiceBean is initialized!
Get the database connection and initialize other beans!
close the database connection!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 23.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics