1. 程式人生 > >第11章—常用註解(持續更新中)

第11章—常用註解(持續更新中)

導入 rest class entity auto config cati param tee

spring boot 系列學習記錄:http://www.cnblogs.com/jinxiaohang/p/8111057.html

碼雲源碼地址:https://gitee.com/jinxiaohang/springboot

最近一段時間學習了springboot,把熟悉的註解總結一下,這樣可以方便查閱、開發。

部分參考網友總結的,帶*號的註解作用不太確定。 (根據首字母順序排列)


@Autowired

作用:自動導入依賴的bean。


@Bean

作用:等價於XML中配置的bean。


@Column

作用:標識實體類中屬性與數據表中字段的對應關系。


@Component *

作用:把普通pojo實例化到spring容器中,相當於配置文件中的<bean id="" class=""/>


@ComponentScan

作用:表示將該類自動發現(掃描)並註冊為Bean,可以自動收集所有的Spring組件,包括@Configuration類。我們經常使用@ComponentScan註解搜索beans,並結合@Autowired註解導入。


@Controller

作用:用於定義控制器類,在spring 項目中由控制器負責將用戶發來的URL請求轉發到對應的服務接口(service層)。


@Entity

作用:表明這是一個實體類。


@Id

作用:表示該屬性為主鍵。


@PathVariable

作用:將request裏的參數的值綁定到Controller裏的方法參數裏的,使用@PathVariable時,URL是這樣的:http://host:port/path/參數值。


@RequestBody

作用:註解用於Controller的方法參數,根據HTTP Request Header的content-Type的內容(通常是application/json),通過適當的HttpMessageConverter轉換為JAVA類


@RequestMapping

作用:提供路由信息,負責URL到Controller中的具體函數的映射。


@RequestParam

作用:將request裏的參數的值綁定到Controller裏的方法參數裏的,使用@RequestParam時,URL是這樣的:http://host:port/path?參數名=參數值。


@ResponseBody

作用:該註解用於將Controller的方法返回的對象,根據HTTP Request Header的Accept的內容,通過適當的HttpMessageConverter轉換為指定格式(常用於Json、XML)後,寫入到Response對象的body數據區。


@RestController

作用:相對於@ResponseBody和@Controller的合集。


@Service

作用:一般用於修飾service層的組件。


@SpringBootApplication

作用:相當於@EnableAutoConfiguration、@ComponentScan和@Configuration的合集。


@Transient

作用:表示該屬性並非一個到數據庫表的字段的映射,ORM框架將忽略該屬性。

第11章—常用註解(持續更新中)