Spring核心框架之IoC容器

简介:
首先定义几个关键性的概念:
Beans :在Spring 中,组成程序主体的并由Spring IoC 容器管理的对象,称为Beans Beans 以及任何与之存在依赖关系的物件,均被反射在由容器使用的Configuration Metadata 中。

IoC容器的核心    
org.springframework.beans org.springframework.context这两个包构成了Spring框架的IoC容器的核心。其中BeanFactory接口提供了能管理任何对象的高级配置机制。但是目前一般不直接使用BeanFactory类,而是用ApplicationContext替代之。ApplicationContextBeanFactory接口的派生,可以看作是BeanFactory的超集。它能够更容易的与Spring AOP特性集成,拥有消息资源处理(Message Resources Handling)和事件发布(Event Publication)等功能。
  ApplicationContext接口代表了Spring IoC容器。主要负责初始化,配置,组装Beans。这个容器从Configuration Metadata 中得到相关的指令。Configuration Metadata 一般是一个XML,JAVA 注解,或者JAVA 代码。
下面是一个典型的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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
< bean  id ="..."  class ="..." > 
<! -- collaborators and configuration for this bean go here -- > 
</bean> 
< bean  id ="..."  class ="..." > 
<! -- collaborators and configuration for this bean go here -- > 
</bean> 
</beans> 

ApplicationContext接口主要有两种实现类,分别是 ClassPathXmlApplicationContextFileSystemXmlApplicationContext 从命名上很容易看出它们的适用范围。
初始化一个 ApplicationContext是极其容易的事情。下面是一般的初始化方式:
ApplicationContext context = 
new ClassPathXmlApplicationContext( new String[] { "services.xml""daos.xml"}); 

从以上代码可以很容易看出,应用程序可以使用多个 配置文件,这样做的好处是显而易见的。另外,还可以在某一个配置文件中引入其它的配置文件,如下所示:
< beans > 
< import  resource ="services.xml" /> 
< import  resource ="resources/messageSource.xml" /> 
< import  resource ="/resources/themeSource.xml" /> 
< bean  id ="bean1"  class ="..." /> 
< bean  id ="bean2"  class ="..." /> 
</beans> 

Beans 还可以通过静态工厂方法来初始化,例如:类的定义代码是:
public  class ClientService { 
private  static ClientService clientService =  new ClientService(); 
private ClientService() {} 
public  static ClientService createInstance() { 
return clientService; 


在配置文件中定义为:
<bean id="clientService" 
class="examples.ClientService" 
factory-method="createInstance"/> 

Beans
在容器内部,Beans BeanDefinition 对象表示。BeanDefinition 对象包含了以下一些信息:
- 完整的类名称,包含包的路径。
-Bean 行为的相关元素,说明了Bean 在容器内的行为。
-Bean 相关的引用,也就是依赖的其它的类。
-Bean 其它的设置信息。

依赖注入 (Dependency Injection)
Spring DI 主要有两种形式:基于构造方法的和基于Setter 方法的。
对于基于构造方法的,例如下面这个类:
package x.y; 
public  class Foo { 
public Foo(Bar bar, Baz baz) { 
// ... 


 
则配置文件应做如下配置:
< beans > 
< bean  id ="foo"  class ="x.y.Foo" > 
< constructor-arg  ref ="bar" /> 
< constructor-arg  ref ="baz" /> 
</ bean > 
< bean  id ="bar"  class ="x.y.Bar" /> 
   < bean  id ="baz"  class ="x.y.Baz" /> 
</ beans > 
对于参数是Bean 的时候,这种方法是可以的。但是如果参数里面含有基本数据类型,比如int 或者String ,那么Spring 便无法确定如何解析。必须采用以下的方法对待:
package examples; 
   public  class ExampleBean { 
   // No. of years to the calculate the Ultimate Answer 
   private  int years; 
   // The Answer to Life, the Universe, and Everything 
   private String ultimateAnswer; 
   public ExampleBean( int years, String ultimateAnswer) { 
     this.years = years; 
     this.ultimateAnswer = ultimateAnswer; 
  } 
}    
在配置文件中配置:
< bean  id ="exampleBean"  class ="examples.ExampleBean" > 
   < constructor-arg  type ="int"  value ="7500000" /> 
   < constructor-arg  type ="java.lang.String"  value ="42" /> 
</ bean > 
或者
< bean  id ="exampleBean"  class ="examples.ExampleBean" > 
   < constructor-arg  index ="0"  value ="7500000" /> 
   < constructor-arg  index ="1"  value ="42" /> 
</ bean > 
对于基于Setter 方法的:
例如下面这个类:
public  class ExampleBean { 
   private AnotherBean beanOne; 
   private YetAnotherBean beanTwo; 
   private  int i; 
   public  void setBeanOne(AnotherBean beanOne) { 
     this.beanOne = beanOne; 
  } 
   public  void setBeanTwo(YetAnotherBean beanTwo) { 
     this.beanTwo = beanTwo; 
  } 
   public  void setIntegerProperty( int i) { 
     this.i = i; 
  } 

则在配置文件中应做如下配置:
< bean  id ="exampleBean"  class ="examples.ExampleBean" > 
  <!--  setter injection using the nested <ref/> element --> 
   < property  name ="beanOne" > < ref  bean ="anotherExampleBean" /> </ property > 
  <!--  setter injection using the neater 'ref' attribute --> 
   < property  name ="beanTwo"  ref ="yetAnotherBean" /> 
   < property  name ="integerProperty"  value ="1" /> 
</ bean > 
< bean  id ="anotherExampleBean"  class ="examples.AnotherBean" /> 
< bean  id ="yetAnotherBean"  class ="examples.YetAnotherBean" /> 
以上例子均摘自 Spring 官方参考文档。
附Spring 3框架结构图









本文转自 kevx 51CTO博客,原文链接:http://blog.51cto.com/spinlock/323821,如需转载请自行联系原作者
目录
相关文章
|
5月前
|
安全 Java Ruby
我尝试了所有后端框架 — — 这就是为什么只有 Spring Boot 幸存下来
作者回顾后端开发历程,指出多数框架在生产环境中难堪重负。相比之下,Spring Boot凭借内置安全、稳定扩展、完善生态和企业级支持,成为构建高可用系统的首选,真正经受住了时间与规模的考验。
418 2
|
4月前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
6月前
|
XML JSON Java
Spring框架中常见注解的使用规则与最佳实践
本文介绍了Spring框架中常见注解的使用规则与最佳实践,重点对比了URL参数与表单参数的区别,并详细说明了@RequestParam、@PathVariable、@RequestBody等注解的应用场景。同时通过表格和案例分析,帮助开发者正确选择参数绑定方式,避免常见误区,提升代码的可读性与安全性。
|
4月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
4月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
291 8
|
4月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
565 2
|
5月前
|
监控 Kubernetes Cloud Native
Spring Batch 批处理框架技术详解与实践指南
本文档全面介绍 Spring Batch 批处理框架的核心架构、关键组件和实际应用场景。作为 Spring 生态系统中专门处理大规模数据批处理的框架,Spring Batch 为企业级批处理作业提供了可靠的解决方案。本文将深入探讨其作业流程、组件模型、错误处理机制、性能优化策略以及与现代云原生环境的集成方式,帮助开发者构建高效、稳定的批处理系统。
628 1
|
6月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
1026 108