Spring 5.0.8.RELEASE文档 Core1-1.2.3 使用容器

版权声明:mj1001001 https://blog.csdn.net/qq_42786889/article/details/81669034

The ApplicationContext is the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. Using the method T getBean(String name, Class requiredType) you can retrieve instances of your beans.
The ApplicationContext enables you to read bean definitions and access them as follows:

ApplicationContext 是一个设计先进的、能管理和注册各种各样的bean以及其依赖的工厂类接口的子接口。你可以抽取你的bean实例通过使用 T getBean(String name, Class requiredType)方法。
ApplicationContext 让你能够读取bean所定义的东西和访问它们:(如下)

// 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();

With Groovy configuration, bootstrapping looks very similar, just a different context implementation class which is Groovy-aware (but also understands XML bean definitions):

读取groovy形式的配置,引导(读取)起来也是非常简单的,仅仅需要更换context的实现类,它是理解groovy的(理解groovy化的xml的bean的定义)

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

The most flexible variant is GenericApplicationContext in combination with reader delegates, e.g. with XmlBeanDefinitionReader for XML files:

最灵活的是GenericApplicationContext ,它能根据后缀自动绑定reader,既能读取xml也能读取groovy文件。读取xml文件:

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

GroovyBeanDefinitionReade去读groovy文件:

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

Such reader delegates can be mixed and matched on the same ApplicationContext, reading bean definitions from diverse configuration sources, if desired.

这个reader委托者能够在同一个ApplicationContext中混合使用,如果需要可以从不同的配置数据源中reading 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 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, allowing you to declare a dependency on a specific bean through metadata (e.g. an autowiring annotation).

你可以使用 getBean 方法来获取实例。ApplicationContext 接口还有几个其他的方法来抽取beans,但是理想情况下你不应该使用它们,甚至你的代码中不应该出现调用getBean()方法,因为这样没用利用到Spring框架的API(没有利用好Spring)。举个例子,Spring集成web框架提供了依赖注入的能力,允许你通过声明注解,然后它就自动装配好,而不需要自己一个一个去getBean。-,-

猜你喜欢

转载自blog.csdn.net/qq_42786889/article/details/81669034