What happens if auto-wiring a non-existing bean?
NoSuchDefinitionException thrown
What injection modes are supported via @Autowired?
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?
2. Want subclass to inherited parent’s setters automatically
When should use constructor DI?
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?
What are the resolution rules for @Autowired?
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?
2. myUserService
How to auto-wire a property to the constructor parameter?
@Autowired
MyConstructor(@Value(“${property}”) property) {}
When is a @Lazy bean created?
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?
Ways to add a method that runs after the construction done?
Note: @PostConstruct is in javax.annotation, JSR-250
Ways to add a method that runs before destruction?
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?
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?