Java > Spring Framework > Spring Core > Spring IoC Container
Annotation-Based Spring IoC Configuration
This snippet demonstrates Spring IoC configuration using annotations instead of XML. It utilizes @Component, @Autowired, and @Configuration annotations to define and wire beans.
Component Definition (@Component)
The @Component annotation marks the MyService class as a Spring bean. The Spring container will automatically detect and register this class as a bean.
package com.example;
import org.springframework.stereotype.Component;
@Component
public class MyService {
private String message = "Hello, Spring IoC (Annotations)!";
public String getMessage() {
return message;
}
public void doSomething() {
System.out.println(message);
}
}
Dependency Injection (@Autowired)
The @Autowired annotation is used for dependency injection. In this case, the MyController class depends on MyService. The Spring container will automatically inject an instance of MyService into the MyController's constructor.
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MyController {
private final MyService myService;
@Autowired
public MyController(MyService myService) {
this.myService = myService;
}
public void processRequest() {
System.out.println("Controller processing request...");
myService.doSomething();
}
}
Configuration Class (@Configuration)
The @Configuration annotation marks the AppConfig class as a configuration class. The @ComponentScan annotation tells Spring to scan the specified package (com.example) for components (classes annotated with @Component, @Service, @Repository, etc.).
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class AppConfig {
}
Main Application
This is the main application class that loads the Spring configuration using AnnotationConfigApplicationContext, which reads configuration from the AppConfig class. It retrieves the MyController bean from the container and calls its processRequest method.
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyController controller = context.getBean(MyController.class);
controller.processRequest();
}
}
Concepts Behind the Snippet
This snippet demonstrates annotation-based configuration for the Spring IoC container. The @Component annotation marks a class as a bean, @Autowired performs dependency injection, and @Configuration defines a configuration class. @ComponentScan enables Spring to automatically detect and register beans within a specified package.
Real-Life Use Case
Annotation-based configuration is commonly used in modern Spring applications. It's cleaner and more concise than XML configuration, especially for complex applications with many beans and dependencies. It promotes a more code-centric approach to configuration.
Best Practices
Use constructor injection with @Autowired for mandatory dependencies, making the dependencies clear and immutable. Use @Qualifier to resolve ambiguity when multiple beans of the same type are available. Keep your component scanning packages well-defined to avoid unintended bean registration.
Interview Tip
Be prepared to explain the benefits of annotation-based configuration over XML configuration. Understand the different types of annotations used for dependency injection (constructor, setter, field). Know the purpose of @ComponentScan and how it relates to automatic bean discovery.
When to Use Them
Annotation-based configuration is suitable for almost all Spring applications, especially those that are new or undergoing modernization. It's generally preferred over XML configuration due to its conciseness and readability.
Memory footprint
Similar to XML configuration, the memory footprint depends on the number of beans and their dependencies. Annotation-based configuration itself doesn't significantly impact memory usage compared to XML. The key factor remains the size and complexity of the application context.
Alternatives
While annotation-based configuration is the most common approach, you can still use XML configuration or a combination of both. Another alternative is Java-based configuration using @Bean annotations within a @Configuration class, which provides more control over bean creation.
Pros
Cons
FAQ
-
What is the purpose of the
@Componentannotation?
The@Componentannotation marks a class as a Spring bean, allowing the Spring container to automatically detect and register it. -
How does
@Autowiredwork?
The@Autowiredannotation is used for dependency injection. Spring will attempt to resolve and inject the required dependency into the field, constructor, or setter method. -
What does
@ComponentScando?
The@ComponentScanannotation tells Spring to scan the specified package for components (classes annotated with@Component,@Service,@Repository, etc.) and register them as beans.