我們再實際開發過程中一般大致爲三層:controller/service/dao或者repository。其中本文最要是分享controller層相關的註解使用。常用的註解有:
@RestController
@RequestMapping
@PathVariable
@RequestParam
@RequestBody
@RestController
先看源碼:
package org.springframework.web.bind.annotation; //...無關多餘的省略 @Controller @ResponseBody public @interface RestController { @AliasFor(annotation = Controller.class) String value() default ""; }
認真點看,這個註解上有兩個曾經使用過的註解,一個@Controller和@ResponseBody。另外網上有人說@RestController是springboot的註解,這裏得說清楚一下,並不是這樣滴。
跟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後端技術棧"); } }
ok,以上已經成功,我們再回到@ResController換成@Controller將會是什麼結果,
@Controller public class RestControllerDemoController { @PostMapping("/getUserById") public Object getUserById(Integer id) { return new User(id, "Java後端技術棧"); } }
測試
再把代碼的方法上加上@ResponseBody。
@Controller public class RestControllerDemoController { @PostMapping("/getUserById") @ResponseBody public Object getUserById(Integer id) { return new User(id, "Java後端技術棧"); } }
再測試:
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
@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容器中進行管理。