`
zqj15011
  • 浏览: 8284 次
  • 性别: 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
分享到:
评论

相关推荐

    Spring扫描器—spring组件扫描使用详解

    一、Spring组件扫描原理 Spring组件扫描的原理基于Java的注解处理和反射机制。它会遍历指定包及其子包下的所有类,寻找带有特定注解(如@Service、@Component、@Repository、@Controller等)的类,并将这些类实例...

    NSpring组件以及文档和dom

    1. **NSpring组件**: - **Spring.Core**: 这是NSpring的基础模块,包含DI容器和基本的AOP支持。DI允许你将对象的依赖关系外部化,使得代码更易于测试和维护。 - **Spring.Aop**: 提供了AOP框架,允许你在不修改源...

    如何写好一个Spring组件的实现步骤

    Spring 组件实现步骤详解 在 Spring 框架中,组件的实现是一个非常重要的步骤,它可以帮助开发者更好地理解和使用 Spring 框架的各种组件。下面,我们将详细介绍如何写好一个 Spring 组件的实现步骤。 首先,需要...

    Web项目中获取SpringBean与在非Spring组件中获取SpringBean.pdf

    然而,有时我们需要在非Spring管理的组件或者非Spring环境下获取Spring管理的Bean。这时,我们就需要一种方式来访问Spring的ApplicationContext,它是Spring容器的核心,负责管理所有Bean。 一种常见的做法是创建一...

    Spring组件开发模式支持SPEL表达式

    Spring组件开发模式支持SPEL表达式 Spring框架作为Java企业级应用程序的主流框架,提供了强大的组件开发模式,支持SPEL(Spring Expression Language)表达式,使得开发者能够更加灵活地使用表达式来实现业务逻辑。...

    Spring组件自动扫描详解及实例代码

    总而言之,Spring组件自动扫描机制极大地简化了Spring项目的配置,提高了开发效率。通过理解和应用@Component、@Repository、@Service和@Controller等注解,开发者可以让Spring自动地管理应用程序中的各种组件,从而...

    spring-struts1-strust2-hibernate 核心包介绍

     除了spring.jar文件,Spring还包括有其它13个独立的jar包,各自包含着对应的Spring组件,用户可以根据自己的需要来选择组合自己的jar包,而不必引入整个spring.jar的所有类文件。 (1) spring-core.jar 这个...

    spring-common:[已弃用]与Spring Boot应用程序一起使用的通用Spring组件库和相关类

    我的Spring Boot应用程序中使用的通用Spring组件库和相关类库 介绍 这个Java库提供了一些与Spring Boot应用程序一起使用的组件。 班级 ResourceReader 用于将文件从Resource读取到字符串中 @Log 注释类,用于注入...

    Web项目中获取SpringBean与在非Spring组件中获取SpringBean.docx

    ...

    SpringCloud组件面试题目_SpringCloud组件面试_springcloud_

    在准备SpringCloud组件面试时,理解并掌握各个关键组件的功能和使用是至关重要的。SpringCloud作为微服务架构的主流框架,包含了许多子项目,每个子项目都有其独特的用途。以下是一些可能在面试中出现的知识点,涵盖...

    Spring Cloud 各组件Demo

    Spring Cloud 各组件Demo ,包含 Spring Cloud Eureka ,Spring Cloud Zuul , Spring Cloud Ribbon , Hystrix-Dashboard-Turbine 如有错误 ,请于本人联系 ,自会及时修改 , 防止误导他人

    Spring_IOC-v(上)笔记

    Spring_IOC-v(上)笔记是关于 Spring 框架中的 IoC(控制反转)技术的笔记,主要介绍了 IoC 的概念、依赖注入、Bean 的设置、Spring 组件的管理等知识点。 IoC 技术是 Spring 框架的核心概念之一,它的主要思想...

    学习笔记:尚硅谷Spring6基础篇

    ### 学习笔记:尚硅谷...- **Spring Test**:支持Spring组件的单元测试和集成测试。 通过这些模块,Spring框架为开发者提供了全面的技术支持和服务,使得开发人员能够更加高效、灵活地构建高质量的企业级应用程序。

    spring cloud各组件实例

    下面将详细讲解 Spring Cloud 的各个组件及其实例。 1. **Eureka**:它是服务注册与发现的核心组件。Eureka Server 提供了 RESTful API 来注册和发现服务。服务提供者启动时会向 Eureka 注册自己的信息,而服务消费...

    使用 Spring Boot 快速构建 Spring 框架应用

    通过自动管理依赖关系,Spring Boot 可以确保引入的库版本与 Spring 框架和其他组件兼容,从而避免了版本冲突的问题。 在自动配置方面,Spring Boot 根据项目依赖自动配置 Spring 组件。例如,如果项目包含了 ...

    spring核心jar包

    它简化了处理HTTP请求、渲染视图以及与其他Spring组件集成的过程。 9. **Spring WebFlux**: 是Spring的新特性,用于构建响应式非阻塞Web应用程序。它支持Reactive Streams API,并且可以与Spring MVC共存。 压缩包...

    spring-5.3.9-dist.zip

    它提供了请求处理、视图解析、模型绑定等功能,与其他Spring组件紧密集成,如Spring Security用于安全控制,Spring Web Flow用于管理复杂用户交互流程。 Spring 5.3.9 版本可能包含以下改进: 1. 性能优化:Spring...

    spring-modules-0.9.zip

    Spring Modules是针对Spring框架的一系列扩展工具和模块,旨在增强Spring的功能并促进其与其他...因此,在现代项目中,虽然Spring Modules的历史价值不容忽视,但可能需要考虑使用更新的Spring组件来满足当前的需求。

    SpringCloud项目实战各组件源代码案例

    SpringCloud微服务远程调用组件Feign的使用 springcloud-circuitbreaker.zip springcloud-config.zipspringcloud-config-oracle.zip springcloud-config-oracle-bus-kafka.zipspringcloud-feign.zip springcloud-...

Global site tag (gtag.js) - Google Analytics