20-Spring Container Flashcards
Stereotype annotations
Component Repository Service Controller RestController Configuration
Meta-annotation
Annotation can be used to create new annotations e.g. @Service, @MockBean. Example:
@Service
public @interface MyService {}
Bean initialization process
- All instances of BeanPostProcessor have a chance to process before and after initialization
- Bean created, properties and dependencies are set
- @PostConstruct
- afterPropertiesSet if a bean implements InitializingBean interface
- init-method in
- ## Ready to useNote: If the same init method has already been invoked, it will not be invoked again
Bean deconstruction process
- @PreDestroy
- destroy (not dispose) method in DisposeableBean bean
- ## destroy-method inNote: If the same destruction method has already been invoked, it will not be invoked again
What are the use cases of spring.factories file?
- Register application event listeners
2. Locate auto-configuration candidates
What is the name of objects created by the Spring IoC container?
Beans
Which of the following are valid ways of defining Spring bean metadata?
- Java annotation
- Java code
- XML fields
Why can I say “lifecycle management in Spring is consistent”?
regardless of the type of application - the way Spring behaves in a unit test is identical to how it will behave in production
Why can I say “lifecycle management in Spring is lightweight”?
It does not require deploying applications to a servlet container or other special environment
How to modify bean definitions?
- Implement BeanFactoryPostProcessor
- Override BeanFactoryPostProcessor.postProcessBeanFactory(…)
- Create a static bean method so that it can be detected before other beans
What does PropertySourcesPlaceholderConfigurer do?
A BFPP that evaluates @Value(“expression”) and
List out 3 BFPP
- PropertySourcesPlaceholderConfigurer
- DeprecatedBeanWarner
- CustomScopeConfigurer
What bean enables @PostConstruct, how is it implemented?
CommonAnnotationBeanPostProcessor extends BeanPostProcessor
Override postProcessBeforeInitialization
When does BeanPostProcessor.postProcessBeforeInitialization get called?
bean creation –> DI –> postProcessBeforeInitialization –> initializers called (e.g. InitializingBean’s afterPropertiesSet or a custom init-method)
When does BeanPostProcessor.postProcessAfterInitialization get called?
bean creation –> DI –> postProcessBeforeInitialization –> initializers called (e.g. InitializingBean’s afterPropertiesSet or a custom init-method) –> postProcessAfterInitialization