Spring Framework 5.2.2 文檔中文翻譯版之容器使用

特別說明

這是一個由simviso團隊所組織進行的基於Spring Framework 5.2.2版本基礎文檔翻譯。若是想要深刻討論,可掃描下方二維碼,加入官方羣和知秋的知識星球,免費給你們分享相關知識。java

因爲專業文檔翻譯難度比較大,咱們內部本着翻譯質量,也有一系列的規範,也因這些規範,消耗的時間更多,同時咱們本身時間也是有限的,做爲公益組織,當下也沒什麼收益,都是小夥伴天天晚上熬夜在作事情,請勿催更,望理解。web

本節參與人員spring


知秋-掘金博客bootstrap

https://juejin.im/user/59c764...segmentfault

image.png

容器使用(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.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進行依賴聲明。

相關文章
相關標籤/搜索