Annotation-based configuration
Annotation-based configuration in Spring MVC refers to the use of annotations to configure the components of a Spring MVC application, such as controllers, request mappings, and bean definitions.
Annotation-based configuration provides a convenient and concise way to configure Spring MVC components compared to XML-based configuration.
Some common annotations used in annotation-based configuration in Spring MVC are:
@Controller
: Used to define a class as a controller.
Example:
@Controller
public class MyController {
...
}
@RequestMapping
: Used to define a mapping between a URL and a method in a controller.
Example:
@Controller
public class MyController {
@RequestMapping("/handleRequest")
public String handleRequest(Model model) {
...
}
}
In the example above, the handleRequest
method is mapped to the URL /handleRequest
.
@Autowired
: Used to automatically inject a bean into a property of another bean.
Example:
@Controllerpublic class MyController { private MyService myService; ... }
In the example above, the MyService
bean is automatically injected into the myService
property of the MyController
bean.
These are some of the most commonly used annotations in annotation-based configuration in Spring MVC. By using annotations, you can easily configure your Spring MVC components, making your code more concise and maintainable.
Leave a Comment