16-Annotation-Driven Configuration Flashcards

1
Q

What happens if auto-wiring a non-existing bean?

A

NoSuchDefinitionException thrown

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

What injection modes are supported via @Autowired?

A
  1. Field
  2. Constructor
  3. ## Setter/method methodNote: although you can auto-wire methods other than setters, this is probably not a good idea, because you are starting to run code during start-up
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

With constructor, in what case we can drop @Autowired?

A

If it’s the only constructor in the component

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

How to inject an optional bean?

A

Using @Autowired(required = false)

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

What is the recommended place to check if an optional bean has been set or not?

A

In @PostConstruct void method() {}

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

When should use setter DI?

A
  1. Circulate dependencies

2. Want subclass to inherited parent’s setters automatically

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

When should use constructor DI?

A
  1. Mandatory dependencies
  2. Immutable
    Constructor injection is generally preferred
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given 2 beans that implement the same service interface e.g. UserService.
Inject bean with @Autowired void method(UserService)
What happens? How to fix it?

A
  1. NoSuchDefinitionException because of 2 beans with the same type
  2. Using void method(@Qualifier(“name”) UserService)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the resolution rules for @Autowired?

A
  1. First, search by type
  2. If there are multiple beans of the same type, it looks for @Qualifier
  3. Match by bean-name and field-name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the equivalent @Autowired in JSR-330?

A

@Resource

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

What are the resolution rules for @Resource in JSR-250?

A

Name first, falling back to type if required.

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

Can @Resource use in constructor DI?

A

@Resource cannot be used on a constructor.

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

What is the bean name for the following cases?

  1. @Service class UserServiceImpl implements UserService {}
  2. @Bean UserService myUserService() {}
A
  1. userServiceImpl

2. myUserService

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

How to auto-wire a property to the constructor parameter?

A

@Autowired

MyConstructor(@Value(“${property}”) property) {}

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

When is a @Lazy bean created?

A
  1. When dependency injected

2. By ApplicationContext.getBean() method

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

What is the default value of @Lazy.value?

A

false

Beans are normally created on startup when application context created

17
Q

If there’re multiple constructors, which one will be invoked?

A
  1. The one with @Autowired. If there’s no @Autowired constructor
  2. Invoke the default constructor (no arguments)
18
Q

Ways to add a method that runs after the construction done?

A
  1. create a method with no params and only return void
  2. mark with @PostConstruct or init-method in XML
    or add @Bean(initMethod=”methodName”) if it’s with @Bean. Only available in @Bean

Note: @PostConstruct is in javax.annotation, JSR-250

19
Q

Ways to add a method that runs before destruction?

A
  1. create a method with no params and only return void
  2. mark with @PreDestroy or destroy-method in XML
    or add @Bean(destroyMethod=”methodName”) if it’s with @Bean. Only available in @Bean

Note: @PreDestroy is in javax.annotation, JSR-250

20
Q

What is the result of SpringApplication.run?

A

ConfigurableApplicationContext (not ApplicationContext)

21
Q

Ways to shut down a Spring application?

A
  1. ConfigurableApplicationContext.close()

2. SpringApplication.exit(ConfigurableApplicationContext, ExitCodeGenerator…)

22
Q

With @Bean, which methods in the bean does Spring infer to call before deconstruction?

A

If exist in bean: public void close(), public void shutdown()

23
Q

What ConfigurableApplicationContext.registerShutdownHook() does?

A

Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

24
Q

When does JVM shut down?

A
  1. The last non-daemon thread ends
  2. The JVM is interrupted (by using ctrlC or sending SIGINT).
  3. The JVM is terminated (by sending SIGTERM)
  4. One of the threads calls System.exit() or Runtime.exit().
25
Q

List out 6 stereotype annotations

A
  1. @Component
  2. @Controller
  3. @RestController
  4. @Service
  5. @Repository
  6. @Configuration

Note that other Spring projects provide their own stereotype annotations
@Bean is not a stereotype annotation

26
Q

Is @Bean a stereotype annotation?

A

No

27
Q

What is meta-annotation?

A

Annotation which can be used to annotate other annotations

28
Q

When creating a new annotation, what RetentionPolicy should be set so that Spring can scan it?

A

Note:
CLASS
Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
RUNTIME
Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.
SOURCE
Annotations are to be discarded by the compiler.

29
Q

What is the execution order of post-construction methods?

A

@PostConstruct –> afterPropertiesSet (if impl InitializingBean) –> init-method

30
Q

What is the execution order of pre-destruction methods?

A

@PreDestroy –> detroy (if impl DisposableBean) –> destroy-method
Note that if it’s an @Bean object, public void close() and public void shutdown() can be considered as default destroy-method

31
Q

In JSR-330, what does @Named do?

A

Equivalent to @Component + @Qualifier