1.2.3. Using the Container 使用容器

Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

 Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.2.1. Configuration Metadata (配置元数据)

1.2.2. Instantiating a Container(实例化一个容器)

1.2.2.1 Composing XML-based Configuration Metadata(组合基于XML的配置元数据)

1.2.2.2 The Groovy Bean Definition DSL (Groovy Bean定义DSL)

1.2.3. Using the Container (使用容器)

1.3. Bean Overview (Bean概览)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.2.3. Using the Container 使用容器

The ApplicationContext is the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. By using the method T getBean(String name, Class<T> requiredType), you can retrieve instances of your beans.

The ApplicationContext lets you read bean definitions and access them, as the following example shows:

ApplicationContext 是高级工厂接口,能够维护不同bean及其依赖项的注册表(registry)。通过使用方法T getBean(String name, Class<T> requiredType),您可以检索bean的实例。

ApplicationContext 允许您读取bean定义并访问bean,如下例所示:

Java

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance
List<String> userList = service.getUsernameList() ;

Kotlin

import org.springframework.beans.factory.getBean

// create and configure beans
val context = ClassPathXmlApplicationContext("services.xml", "daos.xml")

// retrieve configured instance
val service = context.getBean<PetStoreService>("petStore")

// use configured instance
var userList = service.getUsernameList() 

With Groovy configuration, bootstrapping looks very similar. It has a different context implementation class which is Groovy-aware (but also understands XML bean definitions). The following example shows Groovy configuration:

对于Groovy配置,引导(bootstrapping)看起来非常相似。它有一个不同的上下文实现类,它是Groovy感知的(Groovy-aware)(但也理解XMLBean定义)。以下示例展示Groovy配置:

Java

ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", "daos.groovy");

Kotlin

val context = GenericGroovyApplicationContext("services.groovy", "daos.groovy")

The most flexible variant is GenericApplicationContext in combination with reader delegates — for example, with XmlBeanDefinitionReader for XML files, as the following example shows:

最灵活的变体是GenericApplicationContext reader委托的组合,例如,使用XML文件的XmlBeanDefinitionReader,如下例所示:

Java

GenericApplicationContext context = new GenericApplicationContext() ;

new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");

context.refresh() ;

Kotlin

val context = GenericApplicationContext()

XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml")

context.refresh()

You can also use the GroovyBeanDefinitionReader for Groovy files, as the following example shows:

您还可以将GroovyBeanDefinitionReader 用于Groovy文件,如下例所示:

Java

GenericApplicationContext context = new GenericApplicationContext() ;

new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");

context.refresh() ;

Kotlin

val context = GenericApplicationContext()

GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy")

context.refresh()

You can mix and match such reader delegates on the same ApplicationContext, reading bean definitions from diverse configuration sources.

You can then use getBean to retrieve instances of your beans.

您可以在同一个ApplicationContext上混合和匹配这样的reader委托(reader delegate),从不同的配置源读取bean定义。

然后,可以使用getBean 检索bean的实例。

The ApplicationContext interface has a few other methods for retrieving beans, but, ideally, your application code should never use them. Indeed, your application code should have no calls to the getBean()  method at all and thus have no dependency on Spring APIs at all. For example, Spring’s integration with web frameworks provides dependency injection for various web framework components such as controllers and JSF-managed beans, letting you declare a dependency on a specific bean through metadata (such as an autowiring annotation).

ApplicationContext 接口有一些其他方法用于检索bean,但是,理想情况下,应用程序代码不应该使用这些方法。实际上,您的应用程序代码应该根本没有对getBean() 方法的调用,因此完全不依赖于Spring API。例如,Springweb框架的集成为各种web框架组件(Spring’s integration with web framework)(如控制器和JSF管理bean(JSF-managed bean))提供了依赖注入,允许您通过元数据(如自动装配注解(autowiring annotation))声明对特定bean的依赖。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值