What does @ComponentScan do?
It scans packages for classes annotated with stereotype annotations and registers them as Spring beans
By default, which package does @ComponentScan scan?
The package of the class where @ComponentScan is declared and all its subpackages
Why is @SpringBootApplication usually enough for scanning?
It includes:
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
How can you exclude components from scanning?
@ComponentScan(
excludeFilters = @Filter(
type = FilterType.REGEX,
pattern = “*.Repository”
)
)
What happens if a class is excluded from component scanning?
Spring will not register it as a bean
What is the purpose of @Component?
Generic stereotype for Spring-managed components
What is the difference between @Component, @Service, and @Controller?
Functionally identical for bean registration, but semantically indicate architectural roles
What special behavior does @Repository add?
Automatic exception translation into Spring’s DataAccessException
When should you prefer stereotype annotations over @Bean methods?
When you control the class and want simple, concise configuration close to the class
What is a key architecture difference between stereotype annotations and @Bean methods?
What happens if you define a bean with @Bean and also define the same bean via @ComponentScan?
@Bean takes precedence - it overrides the scanned bean
How is a bean named when created via component scanning?
Default name = class name with first letter lowercase
How is a bean named when created via @Bean?
Default name = method name
What happens if the scanned bean and the @Bean method have different names?
They are considered 2 separate beans -> potential ambiguity error
How does Spring resolve parameters of a @Bean method?
“””
@Bean
public OrderService orderService( OrderMapper mapper, OrderRepository repo)
“””
Spring resolves them by type from the application context
What happens if multiple beans of the same type exist when resolving a @Bean method parameter?
Spring throws NoUniqueBeanDefinitionException unless:
- One is @Primary
- A @Qualifier is used
Why does calling another @Bean method inside a configuration class not create multiple instances?
“””
@Bean
public OrderService orderService() {
return new OrderService(orderRepository());
}
“””
Because @Configuration classes are proxied using CGLIB, the proxy intercepts method calls and returns the existing singleton from the context
What would happen if the class were not annotated with @Configuration?
Each method call would create a new instance - no proxy interceptionW
When is @Autowired not required on a constructor?
When the class has exactly one constructor (Spring automatically uses it)
When must you use @Autowired on a constructor?
When there are multiple constructors and you must specify which one to use?
Can @Autowired be used on fields?
Yes, but constructor injection is recommended
Why is constructor injection preferred over field injection?
What does @Autowired(required = false) do?
Spring will inject the dependency if available;
otherwise it leaves it null
What is a better alternative to “required = false”?
Use Optional<T> in constructor parameters</T>