IoC container
27 questions in 8 subtopics.
Inversion of control
What does inversion of control mean in Spring, and how is it different from a class creating its own dependencies or using a service locator?A colleague argues that Spring's IoC container adds hidden magic and that plain constructors with a hand-written main wiring class would be clearer for a small service. When do you agree?Why did dependency injection containers become standard in enterprise Java, and how have newer approaches such as build-time DI and AOT changed the trade-offs?BeanFactory vs ApplicationContext
What doesApplicationContext add on top of BeanFactory?What happens during ApplicationContext.refresh()? Name the main steps in order.A large Spring Boot service takes 45 seconds to start, mostly before the web server is listening. How would you analyse and reduce startup time across your service fleet?Bean definitions
What is aBeanDefinition in Spring, and what information does it hold?Show two ways to register beans programmatically in Spring, for example registering one client bean per entry in a configuration list.You are building an internal library that must create one HTTP client bean per downstream service listed in configuration. Compare doing this with registered bean definitions versus a single factory bean holding a map.Component scanning
What do@Component, @Service, @Repository and @Controller have in common, and what does @Repository add?A @Service class in package com.acme.billing is not found at startup, and the application class is in com.acme.app. Why, and what are the fixes?A shared library uses @ComponentScan to register its beans, and consuming applications get duplicate or unwanted beans. How should the library expose beans instead?Spring's classpath scanning made it easy to add beans, but in large codebases it also makes it hard to know what is in the context. What container standards would you set for a large organisation's Spring services over the next decade?@Configuration and @Bean
What does this print, and why?@Configuration
class AppConfig {
@Bean
Repo repo() {
return new Repo();
}
@Bean
ServiceA a() {
return new ServiceA(repo());
}
@Bean
ServiceB b() {
return new ServiceB(repo());
}
}
@Bean method that returns a PropertySourcesPlaceholderConfigurer causes a warning and @Value fields in the same configuration class are null. Why, and what is the fix?When should you declare beans with @Bean methods instead of annotating the class with @Component?Spring configuration moved from XML to annotations and Java configuration, and now to AOT-generated code. Your company still has large XML configurations in older services. What is your long-term plan for them?ProxyBeanMethods and lite mode
What is the difference between full@Configuration mode and lite mode, and what does proxyBeanMethods = false change?After changing a configuration class to @Configuration(proxyBeanMethods = false), the application has two DataSource connection pools. Explain the code that caused it and the fix.Would you set a standard that all configuration classes in your organisation use proxyBeanMethods = false? What are the arguments?Context hierarchy
What is a parent-childApplicationContext hierarchy, and where did classic Spring MVC applications use one?In a classic Spring MVC application with root and servlet contexts, @Transactional on a service does not work. The service is scanned in the servlet context. Why?When might you deliberately use context hierarchies in a Spring Boot application today, for example with SpringApplicationBuilder.parent and child?A product built on Spring a decade ago loads customer plugins into child application contexts with separate class loaders. The team wants to move to Spring Boot 4. What risks do you assess and what would you redesign?FactoryBean and factory methods
What is a SpringFactoryBean, how is it different from a @Bean factory method, and what does the & prefix in getBean("&myFactory") return?A custom FactoryBean returns null from getObjectType(), and autowiring by type of its product fails or creates the bean too early during startup. Explain why, and how static and instance factory methods in bean definitions compare.You maintain an internal starter that creates a client per configured service using FactoryBean classes written ten years ago. With AOT and native images in scope, would you keep FactoryBeans, switch to @Bean methods, or use bean registration APIs? Explain.No question matches these choices.