16-Annotation-Driven Configuration Flashcards
What happens if auto-wiring a non-existing bean?
NoSuchDefinitionException thrown
What injection modes are supported via @Autowired?
- Field
- Constructor
- ## 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
With constructor, in what case we can drop @Autowired?
If it’s the only constructor in the component
How to inject an optional bean?
Using @Autowired(required = false)
What is the recommended place to check if an optional bean has been set or not?
In @PostConstruct void method() {}
When should use setter DI?
- Circulate dependencies
2. Want subclass to inherited parent’s setters automatically
When should use constructor DI?
- Mandatory dependencies
- Immutable
Constructor injection is generally preferred
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?
- NoSuchDefinitionException because of 2 beans with the same type
- Using void method(@Qualifier(“name”) UserService)
What are the resolution rules for @Autowired?
- First, search by type
- If there are multiple beans of the same type, it looks for @Qualifier
- Match by bean-name and field-name
What is the equivalent @Autowired in JSR-330?
@Resource
What are the resolution rules for @Resource in JSR-250?
Name first, falling back to type if required.
Can @Resource use in constructor DI?
@Resource cannot be used on a constructor.
What is the bean name for the following cases?
- @Service class UserServiceImpl implements UserService {}
- @Bean UserService myUserService() {}
- userServiceImpl
2. myUserService
How to auto-wire a property to the constructor parameter?
@Autowired
MyConstructor(@Value(“${property}”) property) {}
When is a @Lazy bean created?
- When dependency injected
2. By ApplicationContext.getBean() method
What is the default value of @Lazy.value?
false
Beans are normally created on startup when application context created
If there’re multiple constructors, which one will be invoked?
- The one with @Autowired. If there’s no @Autowired constructor
- Invoke the default constructor (no arguments)
Ways to add a method that runs after the construction done?
- create a method with no params and only return void
- 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
Ways to add a method that runs before destruction?
- create a method with no params and only return void
- 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
What is the result of SpringApplication.run?
ConfigurableApplicationContext (not ApplicationContext)
Ways to shut down a Spring application?
- ConfigurableApplicationContext.close()
2. SpringApplication.exit(ConfigurableApplicationContext, ExitCodeGenerator…)
With @Bean, which methods in the bean does Spring infer to call before deconstruction?
If exist in bean: public void close(), public void shutdown()
What ConfigurableApplicationContext.registerShutdownHook() does?
Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.
When does JVM shut down?
- The last non-daemon thread ends
- The JVM is interrupted (by using ctrlC or sending SIGINT).
- The JVM is terminated (by sending SIGTERM)
- One of the threads calls System.exit() or Runtime.exit().
List out 6 stereotype annotations
- @Component
- @Controller
- @RestController
- @Service
- @Repository
- @Configuration
Note that other Spring projects provide their own stereotype annotations
@Bean is not a stereotype annotation
Is @Bean a stereotype annotation?
No
What is meta-annotation?
Annotation which can be used to annotate other annotations
When creating a new annotation, what RetentionPolicy should be set so that Spring can scan it?
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.
What is the execution order of post-construction methods?
@PostConstruct –> afterPropertiesSet (if impl InitializingBean) –> init-method
What is the execution order of pre-destruction methods?
@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
In JSR-330, what does @Named do?
Equivalent to @Component + @Qualifier