Java_Spring_Boot_Interview_1742920115 (2)
Java_Spring_Boot_Interview_1742920115 (2)
@Configuration
• Purpose: Indicates that the class can be used by Spring IoC container as a source of bean
definitions.
• Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
@ComponentScan
• Purpose: Automatically discovers and registers beans in specified packages.
• Example:
@ComponentScan(basePackages = "com.example.myapp")
public class AppConfig {}
@EnableAutoConfiguration
• Purpose: Enables Spring Boot’s auto-configuration mechanism.
• Example:
@EnableAutoConfiguration
public class AppConfig {}
2. Bean Annotations
@Component
• Purpose: Marks a Java class as a Spring-managed component.
• Example:
@Component
public class MyComponent {}
@Service
• Purpose: Specialized @Component annotation, indicating a service layer.
• Example:
@Service
public class MyService {}
@Repository
• Purpose: Specialized @Component annotation for the persistence layer.
• Example:
@Repository
public class MyRepository {}
@Bean
• Purpose: Declares a Spring bean in @Configuration classes.
• Example:
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
@RequestMapping
• Purpose: Maps HTTP requests to handler methods.
• Example:
@RequestMapping("/api")
@Qualifier
• Purpose: Specifies which bean to inject when multiple candidates exist.
• Example:
@Autowired
@Qualifier("specificBeanName")
private MyService myService;
@Value
• Purpose: Injects values from properties files.
• Example:
@Value("${app.name}")
private String appName;
5. Conditional Annotations
@ConditionalOnProperty
• Purpose: Configures beans based on environment properties.
• Example:
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")
public MyFeatureBean myFeatureBean() {
return new MyFeatureBean();
}
@ConditionalOnMissingBean
• Purpose: Defines a bean only if another specific bean is missing.
• Example:
@Bean
@ConditionalOnMissingBean
public MyDefaultBean myDefaultBean() {
return new MyDefaultBean();
}