AnnotationConfigApplicationContext: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 9: Line 9:
=Overview=
=Overview=


AnnotationConfigApplicationContext is a standalone application context that accepts [[@Configuration]]-annotated classes, as well as [[Spring Dependency Injection and Inversion of Control Container Concepts#Stereotype|stereotype]]-annotated classes and JSR-330 compliant classes using javax.inject annotations as input. In case of multiple [[@Configuration|@Configuration]] classes, [[Spring Dependency Injection and Inversion of Control Container Concepts#@Bean|@Bean]] methods defined in later classes will override those defined in earlier classes. This can be leveraged to deliberately override certain bean definitions via an extra [[@Configuration|@Configuration]] class. Component classes can be registered one by one using register(Class), or they can be detected via the [[Spring Dependency Injection and Inversion of Control Container Concepts#Component_Scan|component scan]] mechanism, triggered by scan(String) method. This application context is used by the [[Spring_Boot_Concepts#Spring_Boot_Application_Context|Spring Boot applications]].


'''[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/AnnotationConfigApplicationContext.html AnnotationConfigApplicationContext]''' is a standalone application context that accepts [[#@Configuration|@Configuration]]-annotated classes, as well as [[#Stereotype|stereotype]]-annotated classes and JSR-330 compliant classes using javax.inject annotations as input. In case of multiple [[#@Configuration|@Configuration]] classes, [[#@Bean|@Bean]] methods defined in later classes will override those defined in earlier classes. This can be leveraged to deliberately override certain bean definitions via an extra [[#@Configuration|@Configuration]] class. Component classes can be registered one by one using register(Class), or they can be detected via the [[#Component_Scan|component scan]] mechanism, triggered by scan(String) method. This application context is used by the [[Spring_Boot_Concepts#Spring_Boot_Application_Context|Spring Boot applications]].
=Example=
 
<syntaxhighlight lang='java'>
private static ApplicationContext APPLICATION_CONTEXT = new AnnotationConfigApplicationContext("playground.spring");
...
AComponent c = APPLICATION_CONTEXT.getBean(AComponent.class);
</syntaxhighlight>

Latest revision as of 20:21, 23 November 2018

External

Internal

Overview

AnnotationConfigApplicationContext is a standalone application context that accepts @Configuration-annotated classes, as well as stereotype-annotated classes and JSR-330 compliant classes using javax.inject annotations as input. In case of multiple @Configuration classes, @Bean methods defined in later classes will override those defined in earlier classes. This can be leveraged to deliberately override certain bean definitions via an extra @Configuration class. Component classes can be registered one by one using register(Class), or they can be detected via the component scan mechanism, triggered by scan(String) method. This application context is used by the Spring Boot applications.

Example

private static ApplicationContext APPLICATION_CONTEXT = new AnnotationConfigApplicationContext("playground.spring");
...
AComponent c = APPLICATION_CONTEXT.getBean(AComponent.class);