Core flow defined in AbstractApplicationContext#refresh
.
- collects Bean definitions
- explicitly registered through the ApplicationContext (e.g.
StandardEnvironment
) - declared through
BeanDefinitionRegistryPostProcessor
s (e.g.ConfigurationClassPostProcessor
).
- explicitly registered through the ApplicationContext (e.g.
- (if in a web app) loads the Servlet Context and Servlet Config's "init params"
as
PropertySource
s. - checks that all properties marked as "required" are present.
- prepares the
BeanFactory
- wires in all of the (many) collaborators that the
BeanFactory
uses to load classes, resolve string references of beans to actual beans, property editors, resource loader, app event publisher, and the application context itself.
- wires in all of the (many) collaborators that the
BeanFactoryPostProcessor
s are invoked over registered beans- Three groups:
PriorityOrdered
,Ordered
, and unordered.
- Three groups:
-
Minimum Viable AppCtx (
StaticApplicationContext
) has Six (6) Beans:environment
(aStandardEnvironment
) with it's two sources:systemEnvironment
systemProperties
messageSource
applicationEventMulticaster
lifecycleProcessor
-
An Application Contexts (and friends) scan for special types of beans and registers them:
ApplicationListener
s (byAbstractApplicationContext
)Lifecycle
s (byDefaultLifecycleProcessor
)BeanFactoryPostProcessor
s (byPostProcessorRegistrationDelegate
)BeanPostProcessor
s (byPostProcessorRegistrationDelegate
)LoadTimeWeaverAware
(byAbstractApplicationContext
)
Includes these additional beans (invoked in this order):
-
ConfigurationClassPostProcessor
— aBeanDefinitionRegistryPostProcessor
that registers beans from@Configuration
-annotated classes. -
AutowiredAnnotationBeanPostProcessor
- a
BeanPostProcessor
that injects@Autowired
methods and fields with the corresponding Spring Bean.
- a
-
o.s.c.annotation.internalRequiredAnnotationProcessor
-
CommonAnnotationBeanPostProcessor
- a
BeanPostProcessor
that recognizes a number of JEE annotations:
- a
-
o.s.c.event.internalEventListenerFactory
-
o.s.c.event.internalEventListenerProcessor
Don't confuse ApplicationContext Lifecycle Events with ApplicationEvents: the former describe events on the container itself, especially during start-up; the later describe...
- ApplicationStartedEvent
- ApplicationEnvironmentPreparedEvent
- ApplicationPreparedEvent
- ContextRefreshedEvent
- EmbeddedServletContainerInitializedEvent
- ApplicationReadyEvent
org.springframework.beans
org.springframework.context.annotation.ClassPathBeanDefinitionScanner
Referring to the Spring feature wherein individual beans indicate they themselves have a lifecycle that should be triggered.
Q: these were introduced circa Spring 3... are they useful to know?
Knowing the start-up flow of an Application Context (and how the test harnesses work) can greatly improve your ability to anticipate in what order property sources are searched:
- ...
BeanFactoryPostProcessor
s are executed, including that from theConfigFileApplicationListener
, which is run at near-highest precedence in the "Ordered" group of BFPPs. This BFPP ensures that theapplication.properties
and friends are last in the property source search order.
- For every Profile (in reverse order as named in
spring.profiles.active
, thenspring.profiles.include
, finally the so-called default profile),application-{profile}.properties
, this component searches for that file at the following paths (in this order):file:./config
file:./
classpath:./config
classpath:./
- Since Spring 3.2
SpringApplication
, on initialization, loads via the Spring Factories feature (see also spring.factories).ApplicationContextInitializer
sApplicationListener
s
- Gutter icon treatment only works for
@Component
-registered beans. - IntelliJ
@EnableAspectJAutoProxy(proxyTargetClass = true)
.
org.springframework.core.env.AbstractEnvironment#isProfileActive
implements the@Profile
feature.- while
spring.profiles.active
is implemented here,spring.profiles.include
is actually a Spring Boot feature ()
- while
- When you are specifying a
basePackage
for bean scanning, that value can contain placeholder properties:System.setProperty("rootPackage", "hello"); AnnotationConfigApplicationContext appCtx = new AnnotationConfigApplicationContext(); appCtx.scan("${rootPackage}.simple");
- When Spring loads property files, it actually delegates to the
java.util.Properties
class to do so.- (fun but likely-useless) this means that properties can be loaded in XML format (because
Properties
implements that). Also fun is that the Spring's property loading utility specifically continues to support loading files with the.xml
suffix.
- (fun but likely-useless) this means that properties can be loaded in XML format (because