0
点赞
收藏
分享

微信扫一扫

【刨根问底】在Springboot中MVC的常用注解<上>


我们再实际开发过程中一般大致为三层:controller/service/dao或者repository。其中本文最要是分享controller层相关的注解使用。常用的注解有:

  • @RestController
  • @RequestMapping
  • @PathVariable
  • @RequestParam
  • @RequestBody

【刨根问底】在Springboot中MVC的常用注解<上>_技术栈


@RestController


先看源码:


package org.springframework.web.bind.annotation;	
//...无关多余的省略	
@Controller	
@ResponseBody	
public @interface RestController {	
    @AliasFor(annotation = Controller.class)	
    String value() default "";	
}


认真点看,这个注解上有两个曾经使用过的注解,一个@Controller和@ResponseBody。另外网上有人说@RestController是springboot的注解,这里得说清楚一下,并不是这样滴,请看下面:

【刨根问底】在Springboot中MVC的常用注解<上>_技术栈_02

跟springboot半毛钱关系都没有。回到前面,@Controller是标记这个类为controller类,@ResponseBody是将返回数据类型转换为json格式。所以在类上面加注解@RestController表示这个类是controller类并且方法返回参数均为json格式。但是在使用的时候需要注意,如果涉及到页面渲染或页面跳转的不能使用@RestController,只能使用原始的@Controller来处理,所以一般情况下@RestController的使用场景都是前后端分离。一下是一个使用场景:


public class User {	
    private Integer userId;	
    private String userName;	
// set get  ....省略	
}


@RestController	
public class RestControllerDemoController {	
    @PostMapping("/getUserById")	
    public Object getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}


测试:

【刨根问底】在Springboot中MVC的常用注解<上>_User_03

结果:

【刨根问底】在Springboot中MVC的常用注解<上>_User_04

ok,以上已经成功,我们再回到@ResController换成@Controller将会是什么结果,


@Controller	
public class RestControllerDemoController {	
    @PostMapping("/getUserById")	
    public Object getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}


测试

【刨根问底】在Springboot中MVC的常用注解<上>_技术栈_05

再把代码的方法上加上@ResponseBody。


@Controller	
public class RestControllerDemoController {	
    @PostMapping("/getUserById")	
    @ResponseBody	
    public Object getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}


再测试:

【刨根问底】在Springboot中MVC的常用注解<上>_User_06

ok,证明了@RestController就是@Controller和@ResponseBody的集合体。

官方对@RestController的解释:


This code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.


所以咱们可以这么理解:



@RestController=@Controller+@ResponseBody


既然是两个的集合体,那么我们可以分开去研究,先去看看@Controller。官方解释:


This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. It is typically used in combination with annotated handler methods based on the RequestMapping annotation.


此注解用作@Component的专用化,允许通过类路径扫描自动检测实现类。它通常与基于请求映射注解的带注解的处理程序方法结合使用。

看@Controller源码:


package org.springframework.stereotype;	
@Target({ElementType.TYPE})	
@Retention(RetentionPolicy.RUNTIME)	
@Documented	
@Component	
public @interface Controller {	
    @AliasFor(annotation = Component.class)	
    String value() default "";	
}


可以看到@Controller注解上有个注解@Component,其余三个注解不是我们本次所关心的,想了解请阅读文章


@Controller相当于dispatcherServlet+Model


【刨根问底】在Springboot中MVC的常用注解<上>_技术栈_07

@Controller同目录下的几个注解,并且@Component也在这个目录下。

1,@Controller所用是 控制器(注入服务):用于标注控制层,相当于struts中的action/controller层,

2,@Service作用是 服务(注入):用于标注服务层,主要用来进行业务的逻辑处理,比如:


@Service("userService")	
public UserServiceImpl implements UserService{	
 //....	
}


,然后再对应需要使用的地方注入,


@Resource("userService")	
private UserService userServie;


3,@repository作用是(实现dao访问):用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件。

4,@Component其中前面三个注解上都有@Component注解, (该注解是把普通pojo实例化到spring容器中,相当于配置文件中的 


<bean id="user" class="com.lawt.domain.User"/>)


泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Service等的时候),我们就可以使用@Component来标注这个类。

案例: 


<context:component-scan base-package="com.lawt.*">


上面的这个例子是引入Component组件的例子,其中base-package表示为需要扫描的所有子包并且被扫描的类上必须被


@Controller 、@Service、@Repository 、@Component


 注解中的一个注解,都会把这些类纳入进spring容器中进行管理。

后面继续分析剩下的注解,敬请期待!

举报

相关推荐

0 条评论