Loading

Quipoin Menu

Learn • Practice • Grow

spring / Spring Configuration
interview

Q1. What are the ways to configure Spring beans?
Spring supports three configuration approaches:
• XML-based configuration (using applicationContext.xml)
• Annotation-based configuration (using @Component, @Service, etc., with component scanning)
• Java-based configuration (using @Configuration and @Bean methods)
Modern Spring Boot favors Java config and annotations.

Q2. What is @Configuration and @Bean?
@Configuration indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions.
@Bean is a method-level annotation that tells Spring that the method returns an object to be registered as a bean in the application context.
Example:
@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyService();
    }
}

Q3. How does component scanning work?
Component scanning automatically detects classes annotated with @Component, @Service, @Repository, @Controller and registers them as beans.
You enable scanning with @ComponentScan (Java config) or <context:component-scan> in XML.
Spring scans specified packages for annotated classes.

Q4. What is the difference between @Component, @Service, and @Repository?
All are specializations of @Component for different layers:
@Service for service layer
@Repository for persistence layer (adds translation of persistence exceptions)
@Controller for web layer
They are semantically equivalent but provide clarity and enable specific exception handling or AOP pointcuts.

Q5. What is XML configuration in Spring?
XML configuration uses XML files (e.g., applicationContext.xml) to define beans and their dependencies.
Example:
<bean id="myService" class="com.example.MyService">
    <property name="myDao" ref="myDao"/>
</bean>
Though less common now, it's still used in legacy projects.