Analysis of the Spring MVC Principle

发布于:2023-01-20 ⋅ 阅读:(13) ⋅ 点赞:(0) ⋅ 评论:(0)


Spring MVC framework is one of the most famous and popular frameworks in Java programming. As a professional programmer, it is crucial to understand the principle behind it. This tutorial will briefly introduce Spring MVC and try to replicate the whole framework with POJO Java code. Let's do it!

Components of Spring MVC

Before diving into the concept of MVC, we need first to understand what is MVC:

M(Model): model contains the data of an application that can be transferred within different parts of the MVC system.

C(Controller): the controller is the logic or "brain" of the whole system. We will use some simple servlets to replicate the function of the Spring controller.

V(view): the view represents certain formats that will appear in clients' browsers. Current frequently used view formats are the Jsp and Thymeleaf etc.

Here below is a picture describing what Spring MVC framework looks like;

                               picture source:

Configuration using pure Java

The first step is to set up proper Maven dependencies. Basically, what we need is Servlet, Tomcat and primary Spring context.

            <!-- -->


After adequately inserting our dependencies, we will handle crucial components in this project. Here, we first need to establish our tomcat setting:

public class MainServer {

    private ApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(ApplicationConfig.class);

    public MainServer(){
        Tomcat tomcat = new Tomcat();

        Context context = tomcat.addContext("",null);

        try {
            List<Class> list = ServletUtil.getClasses("com.project.servlet");
            for (Class  c : list){
                    // To obtain objects that are annotated with our own @ServletObj
                    ServletObj servletObj = (ServletObj)c.getAnnotation(ServletObj.class);
        } catch (Exception e) {


    // define a method to register servlet 
    private void registerServlet(Context context,Class servletClass,String urlPath) throws Exception {
        Wrapper servlet = Tomcat.addServlet(
                (Servlet) applicationContext.getBean(servletClass));

    public static void main(String[] args) {
        new MainServer();

First of all, the tomcat port is necessary. Then a context is added for this tomcat. To make things easier, we used a custom annotation called "@ServletObj" to identify which class is the servlet we want to add to our dispatcher. Here is the code of our ServletObj annotation using reflection.

public class ServletUtil {

    // scan the name of packages within typed packageName 
    public static List<Class> getClasses(String packageName){
        List<Class> list = new ArrayList<>();
        // obtain packages' name and replace with the name we want
        String packageDirName = packageName.replace(".","/");
        // obtain full URL
        try {
            URL url = Thread.currentThread().getContextClassLoader()
            String filePath = URLDecoder.decode(url.getFile(),"utf-8");
            // obtain new object through filepath
            File dirFile = new File(filePath);
            String[] classFiles = dirFile.list();
            for(String f : classFiles) {
                    String fileName = f.substring(0,f.lastIndexOf(".class"));
                    Class c = Class.forName(packageName + "." + fileName);
        } catch (Exception e) {
        return list;


The ServletUtil class displays how we can scan through the name of our package and obtain all classes underneath it. In this tutorial, we added servlets to our tomcat context and accessed static resources. For our Spring dispatch configuration, we will leave it to the following tutorial. I can't wait to see you next time!