这是一个由simviso团队所组织进行的基于Spring Framework 5.2.2版本基础文档翻译。若是想要深刻讨论,可扫描下方二维码,加入官方群和知秋的知识星球,免费给你们分享相关知识。java
因为专业文档翻译难度比较大,咱们内部本着翻译质量,也有一系列的规范,也因这些规范,消耗的时间更多,同时咱们本身时间也是有限的,做为公益组织,当下也没什么收益,都是小伙伴天天晚上熬夜在作事情,请勿催更,望理解。
web
本节参与人员spring
知秋-掘金博客bootstrap
https://juejin.im/user/59c764...segmentfault
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.app
ApplicationContext
是一个可以维护不一样bean的注册信息及其依赖关系的高级工厂接口。经过使用方法 T getBean(String name, Class<T> requiredType)
,能够检索查找bean的实例。框架
The ApplicationContext
lets you read bean definitions and access them, as the following example shows:ide
经过 ApplicationContext
,你能够读取bean定义并使用它们,以下所示:flex
Javaui
// 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配置来讲,它的引导方式看起来和咱们以前接触的其余同类引导很是类似。它有一个不同凡响且专为Groovy而设计的上下文实现类(该类一样也能够识别XML中的bean定义)。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文件,也能够是Groovy配置文件)一块儿结合使用。好比,对于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:
一样,针对 Groovy
文件也可使用 GroovyBeanDefinitionReader
,示例以下:
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.
你能够在 ApplicationContext
中混合组合对应匹配 reader
,从不一样的配置源读取bean的定义。
You can then use getBean
to retrieve instances of your beans. 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).
而后,你可使用 getBean
方法去检索你的bean实例。ApplicationContext
接口为检索bean实例也提供了一些其余方法。但理想状况下,你的应用程序代码中应该永远用不到它们。确实,你的应用程序代码应该根本不会调用 getBean()
方法。所以,这里根本不依赖于Spring的API。例如,集成了web框架的Spring对于不一样的web框架组件(例如controller和JSF所管理的bean)提供了依赖注入。让你经过元数据(例如 @Autowired
注解)来对一个特定的bean进行依赖声明。