Overview
Annotations in a Spring application provide a way to configure dependency injection, web controllers, transaction management, and more, using metadata. They simplify the configuration and make the codebase more readable and maintainable by reducing the need for XML configuration files.
Key Concepts
- Dependency Injection: Annotations like
@Autowired
allow Spring to resolve and inject collaborating beans without explicit XML configuration. - Component Scanning: Spring uses annotations like
@Component
,@Service
,@Repository
, and@Controller
to automatically detect and register beans. - Aspect-Oriented Programming (AOP): Annotations such as
@Aspect
,@Before
,@After
, and@Around
enable the declaration of aspects and advice.
Common Interview Questions
Basic Level
- What role do annotations play in Spring?
- How does the
@Autowired
annotation work?
Intermediate Level
- How do you enable component scanning in a Spring application?
Advanced Level
- Can you explain the difference between
@Component
,@Repository
,@Service
, and@Controller
annotations?
Detailed Answers
1. What role do annotations play in Spring?
Answer: Annotations in Spring are used to provide metadata configuration for your application. They simplify the configuration process, allowing for more concise and readable code. Annotations can be used for defining beans, injecting dependencies, configuring transactions, and more.
Key Points:
- Annotations reduce the need for XML configuration.
- They enable automatic component scanning.
- Annotations make the code more readable and easier to maintain.
Example:
// Unfortunately, Spring Framework uses Java for its examples.
// The correct syntax for a Spring annotation in Java would be:
@Component
public class MyComponent {
// Your code here
}
2. How does the @Autowired
annotation work?
Answer: The @Autowired
annotation is used by Spring to perform dependency injection. When you annotate a field, constructor, or method with @Autowired
, Spring automatically resolves the dependency by searching for a matching bean in its application context and injects it.
Key Points:
- @Autowired
can be placed on field, constructor, or setter method.
- It works by type, but can also be configured to work by name.
- If no matching bean is found, it throws an exception, unless required
attribute is set to false.
Example:
// Note: Spring Framework is based on Java, so the example is in Java.
@Service
public class MyService {
private final MyRepository myRepository;
@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
3. How do you enable component scanning in a Spring application?
Answer: Component scanning can be enabled through the @ComponentScan
annotation in your configuration class, or by declaring <context:component-scan>
in your XML configuration. This tells Spring where to look for components, configurations, and services to be automatically detected and registered as beans.
Key Points:
- @ComponentScan
is typically used with @Configuration
classes.
- You can specify base packages to scan.
- It works with annotations like @Component
, @Service
, @Repository
, and @Controller
.
Example:
@Configuration
@ComponentScan(basePackages = "com.example.myapp")
public class AppConfig {
// Additional configuration here
}
4. Can you explain the difference between @Component
, @Repository
, @Service
, and @Controller
annotations?
Answer: These annotations are specializations of @Component
used for specific purposes, helping with automatic component scanning and providing additional semantic meaning:
@Component
: a generic stereotype for any Spring-managed component.@Repository
: indicates a DAO component in the persistence layer.@Service
: marks a service class in the business layer.@Controller
: specifies a controller in the web layer.
Key Points:
- They help in layer-specific component scanning and bean categorization.
- @Repository
can provide persistence-specific support and exception translation.
- @Controller
is typically used in Spring MVC to mark web request handlers.
Example:
@Repository
public class UserRepository {
// Your persistence code here
}
@Service
public class UserService {
// Your business code here
}
@Controller
public class UserController {
// Your web request handling code here
}
Note: The code examples provided are in Java since Spring Framework is primarily used with Java.