20-Spring Container Flashcards

1
Q

Stereotype annotations

A
Component
Repository
Service
Controller
RestController
Configuration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Meta-annotation

A

Annotation can be used to create new annotations e.g. @Service, @MockBean. Example:
@Service
public @interface MyService {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Bean initialization process

A
  1. All instances of BeanPostProcessor have a chance to process before and after initialization
  2. Bean created, properties and dependencies are set
  3. @PostConstruct
  4. afterPropertiesSet if a bean implements InitializingBean interface
  5. init-method in
  6. ## Ready to useNote: If the same init method has already been invoked, it will not be invoked again
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Bean deconstruction process

A
  1. @PreDestroy
  2. destroy (not dispose) method in DisposeableBean bean
  3. ## destroy-method inNote: If the same destruction method has already been invoked, it will not be invoked again
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the use cases of spring.factories file?

A
  1. Register application event listeners

2. Locate auto-configuration candidates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the name of objects created by the Spring IoC container?

A

Beans

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following are valid ways of defining Spring bean metadata?

A
  1. Java annotation
  2. Java code
  3. XML fields
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why can I say “lifecycle management in Spring is consistent”?

A

regardless of the type of application - the way Spring behaves in a unit test is identical to how it will behave in production

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why can I say “lifecycle management in Spring is lightweight”?

A

It does not require deploying applications to a servlet container or other special environment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to modify bean definitions?

A
  1. Implement BeanFactoryPostProcessor
  2. Override BeanFactoryPostProcessor.postProcessBeanFactory(…)
  3. Create a static bean method so that it can be detected before other beans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does PropertySourcesPlaceholderConfigurer do?

A

A BFPP that evaluates @Value(“expression”) and

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List out 3 BFPP

A
  1. PropertySourcesPlaceholderConfigurer
  2. DeprecatedBeanWarner
  3. CustomScopeConfigurer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What bean enables @PostConstruct, how is it implemented?

A

CommonAnnotationBeanPostProcessor extends BeanPostProcessor

Override postProcessBeforeInitialization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When does BeanPostProcessor.postProcessBeforeInitialization get called?

A

bean creation –> DI –> postProcessBeforeInitialization –> initializers called (e.g. InitializingBean’s afterPropertiesSet or a custom init-method)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When does BeanPostProcessor.postProcessAfterInitialization get called?

A

bean creation –> DI –> postProcessBeforeInitialization –> initializers called (e.g. InitializingBean’s afterPropertiesSet or a custom init-method) –> postProcessAfterInitialization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Input, output of BeanPostProcessor.postProcessBeforeInitialization

A
Object postProcessBeforeInitialization(Object originalBean, String beanName);
return post processed bean
17
Q

About JDK proxy

A
  1. Also called dynamic proxies
  2. Built-in in JDK
  3. Requirements: Java interfaces
  4. All interfaces proxied
18
Q

About CGLib proxy

A
  1. Not build-in in JDK
  2. Included in Spring jars
  3. Used when interface is not available
  4. Cannot be applied to final classes or methods
19
Q

How to force dependency order?

A
Using @DependsOn(value[])
Apply to component class and bean method
20
Q
What are type and beanName created in this case?
@Bean
TransferService tfService() {
    return new BankTransferServiceImpl();
}
A

type: TransferService (not BankTransferServiceImpl)
name: tfService

21
Q

Can CGLib prox private methods?

A

No, because it creates subclass of the target class so there’s no way to intercept private