Spring中IOC容器的示例分析(ioc容器,spring,编程语言)

时间:2024-05-02 06:44:18 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

一.Spring的IOC容器概述

Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后容器在创建bean时注入这些依赖关系。Spring实现IOC容器的基础是org.springframework.be和org.springframework.context。



核心接口BeanFactory 接口提供了一种能够管理任何类型对象的高级配置机制。 ApplicationContext 是一个子接口BeanFactory。它增加了与Spring的AOP功能更容易的集成; BeanFactory提供了配置框架和基本功能,并ApplicationContext增加了更多的企业特定功能。
在Spring中,构成应用程序的骨干并由Spring IoC 容器管理的对象称为bean。bean是一个实例化,组装并由Spring IoC容器管理的对象。

Spring如何工作的视图:
Spring中IOC容器的示例分析

二.Spring的IOC的方式

Spring容器xml配置管理
通常是多个bean定义组成。基于XML的配置元数据将这些bean配置为<bean/>顶层元素内的<beans/>元素
如下图提供spring的官方的配置文件模板(素材来源www.spring.io)

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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.xsd">

<beanid="..."class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<beanid="..."class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions go here -->

</beans>


1.默认构造器

当你通过构造函数的方法创建一个bean时,所有的普通类都可以被Spring使用和兼容。也就是说,正在开发的类不需要实现任何特定的接口或以特定的方式编码。只需指定bean类就足够了。但是,根据您用于特定bean的IoC类型,需要一个默认(空)构造函数。

使用基于XML的配置元数据,您可以指定您的bean类如下:

<beanid="exampleBean"class="examples.ExampleBean"/>

<beanname="anotherExample"class="examples.ExampleBeanTwo"/>


其中 bean中的class是我们交于spring初始化的bean的全路径。

2.静态工厂

在定义使用静态工厂方法创建的bean时,可以使用该class 属性来指定包含static工厂方法的类和factory-method指定工厂方法本身的名称的属性。你应该可以调用这个方法(使用后面描述的可选参数)并返回一个实例化对象,这个实例化对象随后被视为是通过构造函数创建的。用于这种bean定义的就是被称为静态工厂方式创建。
静态工厂的定义

publicclassClientService {

privatestaticClientService clientService = newClientService();

privateClientService() {}

publicstaticClientService createInstance() {
returnclientService;
}
}


配置xml

<beanid="clientService"
class="examples.ClientService"
factory-method="createInstance"/>

这样的spring框架在调用IOC实例化的使用是通过反射创建 调用静态工厂的中的static方法去创建对象。此时在创建的对象的是在静态方法手中。

3.实例化工厂

实例化工厂的方式相同于静态工厂方法,只是在一个实例工厂中反射一个存在对象的非静态方法,从而去spring容器去创建实例化的bean。
创建实例化工厂

publicclassDefaultServiceLocator {

privatestaticClientService clientService = newClientServiceImpl();

privatestaticAccountService accountService = newAccountServiceImpl();

publicClientService createClientServiceInstance() {
returnclientService;
}

publicAccountService createAccountServiceInstance() {
returnaccountService;
}
}


配置xml:

<beanid="serviceLocator"class="examples.DefaultServiceLocator">
</bean>

<beanid="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>

<beanid="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/>


 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Spring中IOC容器的示例分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Spring依赖注入的示例分析下一篇:

2 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18