Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of controller does Spring internally implement when @controller is used

Since there are many types of controllers in Spring2.5 version, what kind of controller does Spring internally implement when @controller is used? and how does spring decide what controller to implement?

like image 539
kartheek Avatar asked Feb 03 '26 01:02

kartheek


1 Answers

Short answer: It doesn't matter how @Controller is implemented internally.

Long answer:

First you should read the reference manual to know the API, which is primarly based on annotations. You have no AbstractController, SimpleFormController etc. Important sentence:

The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API.

You must abandon "Spring MVC 2.5 thinking" and just define beans annotated with @Controller like

@Controller
public class ClinicController {

    private final Clinic clinic;

    @Autowired
    public ClinicController(Clinic clinic) {
        this.clinic = clinic;
    }

    @RequestMapping("/")
    public void welcomeHandler() {
    }

    @RequestMapping("/vets")
    public ModelMap vetsHandler() {
        return new ModelMap(this.clinic.getVets());
    }

}

and just use them as ordinary beans (by adding to servlet.xml <bean class="com.example.ClinicController " />). It's much easier this way.

like image 77
Xaerxess Avatar answered Feb 04 '26 15:02

Xaerxess



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!