特别说明
这是一个由simviso团队所组织进行的基于Spring Framework 5.2.2版本基础文档翻译。如果想要深入讨论,可扫描下方二维码,加入官方群和知秋的知识星球,免费给大家分享相关知识。
由于专业文档翻译难度比较大,我们内部本着翻译质量,也有一系列的规范,也因这些规范,消耗的时间更多,同时我们自己时间也是有限的,作为公益组织,当下也没什么收益,都是小伙伴每天晚上熬夜在做事情,请勿催更,望理解。
本节参与人员
知秋-掘金博客
https://juejin.im/user/59c764...
容器使用(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
, you can retrieve instances of your beans.
ApplicationContext
是一个能够维护不同bean的注册信息及其依赖关系的高级工厂接口。通过使用方法 T getBean(String name, Class
,可以检索查找bean的实例。
The ApplicationContext
lets you read bean definitions and access them, as the following example shows:
通过 ApplicationContext
,你可以读取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 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("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进行依赖声明。