Spring的@RequestMapping如何工作?

时间:2014-02-07 19:42:53

标签: java spring aop

如您所知@RequestMapping习惯拦截HttpServletRequest

我想知道@Controller @RequestMapping如何将来自客户端的请求绑定到java类中的特定方法?

我想写一个类似的java应用程序来做同样的功能,想象一下我们有这样一个类:

@Actor
public class JavaForever {

  @Department(value="IT")
  public void departmentIT(){...}

  @Department(value="Physic")
  public void departmentPhysic(){...}
}

还有一个StudentBean类:

public class StudentBean {

    private String department;
    private Integer age;
    //Other class variable
    //Getters & Setters
}

最后我们有一个像这样的Test类:

public class TestApplication {
   //getStudentFromDatabaseMethod() implementation here

   public static void main(String[] agrs){
     List<StudentBean> allStudents = new TestApplication().getStudentFromDatabaseMethod();
   //other codes
   }
}

当您看到getStudentFromDatabaseMethod()返回List< StudentBean>时,现在的问题是我们如何强制此方法拦截@DepartmentJavaForever中的before注释1}}它返回任何值......

我们怎么做?

1 个答案:

答案 0 :(得分:7)

这是一个广泛的概述

  1. 您确定要搜索的Spring类(用于注释)。
  2. Spring找到你的@Controller和@RequestMapping注释。
  3. Spring从@RequestMapping注释构建URL值的映射。
  4. 在运行时,当Spring收到请求时,它会在地图中搜索  URL。当找到URL时,它会调用标记为的方法  @RequestMapping。
  5. 要点:

    • 注释不会任何事情。它们是东西的其他类的标记。

    首先阅读Annotation Tutorial。 您需要扫描您的类(在启动期间)以获取注释(使用反射),然后适当地处理它们。

相关问题