Java 春天是怎样的';s@RequestMapping工作?

Java 春天是怎样的';s@RequestMapping工作?,java,spring,aop,Java,Spring,Aop,正如您所知,@RequestMapping用于拦截HttpServletRequest 我想知道@Controller@RequestMapping如何将来自客户端的请求绑定到java类中的特定方法 我想编写一个类似的java应用程序来实现相同的功能,假设我们有一个类似这样的类: @Actor public class JavaForever { @Department(value="IT") public void departmentIT(){...} @Department

正如您所知,
@RequestMapping
用于拦截
HttpServletRequest

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

我想编写一个类似的java应用程序来实现相同的功能,假设我们有一个类似这样的类:

@Actor
public class JavaForever {

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

  @Department(value="Physic")
  public void departmentPhysic(){...}
}
public class TestApplication {
   //getStudentFromDatabaseMethod() implementation here

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

public class StudentBean {

    private String department;
    private Integer age;
    //Other class variable
    //Getters & Setters
}
最后我们有一个这样的测试类:

@Actor
public class JavaForever {

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

  @Department(value="Physic")
  public void departmentPhysic(){...}
}
public class TestApplication {
   //getStudentFromDatabaseMethod() implementation here

   public static void main(String[] agrs){
     List<StudentBean> allStudents = new TestApplication().getStudentFromDatabaseMethod();
   //other codes
   }
}
公共类测试应用程序{
//getStudentFromDatabaseMethod()实现
公共静态void main(字符串[]agrs){
列出所有学生=NewTestApplication().getStudentFromDatabaseMethod();
//其他代码
}
}
正如您看到的
getStudentFromDatabaseMethod()
返回
List
,现在的问题是,我们如何使用
@Department
注释强制此方法获得截获,该注释位于
JavaForever
中,然后它返回任何值


我们如何做到这一点?

这里是一个大致的概述

  • 您可以标识Spring要搜索(注释)的类
  • Spring会找到@Controller和@RequestMapping注释
  • Spring从@RequestMapping注释构建URL值的映射
  • 在运行时,当Spring收到请求时,它会在映射中搜索 网址。当它找到URL时,它调用用 @请求映射
  • 总结:

    • 注释什么都不做。它们是其他做事情的类的标记
    从阅读一篇文章开始。
    您需要(在启动过程中)扫描类中的注释(使用反射),然后适当地处理它们。

    这太宽泛了。阅读源代码。从
    RequestMappingHandlerMapping
    类开始。Spring的
    @Controller
    处理程序方法不使用AOP。我已经研究过该类,但它有很多额外的方面,这会造成太多的混乱…要回答您的问题,您需要理解并解释该类和其他类中发生的每一件事情,太宽了。Spring的MVC堆栈不是开玩笑的。@SotiriosDelimanolis您以前在这方面写过代码吗???