Spring Boot 最常用的注解,都在這里了!
來源:張伯毅
blog.csdn.net/zhanglong_4444
一、注解(annotations)列表 二、注解(annotations)詳解 三、JPA注解 四、springMVC相關(guān)注解 五、全局異常處理
一、注解(annotations)列表
@SpringBootApplication: 包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan讓spring Boot掃描到Configuration類并把它加入到程序上下文。
@Configuration 等同于spring的XML配置文件;使用Java代碼可以檢查類型安全。
@EnableAutoConfiguration 自動配置。
@ComponentScan 組件掃描,可自動發(fā)現(xiàn)和裝配一些Bean。
@Component 可配合CommandLineRunner使用,在程序啟動后執(zhí)行一些基礎(chǔ)任務(wù)。
@RestController 注解是@Controller和@ResponseBody的合集,表示這是個(gè)控制器bean,并且是將函數(shù)的返回值直 接填入HTTP響應(yīng)體中,是REST風(fēng)格的控制器。
@Autowired 自動導(dǎo)入。
@PathVariable 獲取參數(shù)。
@JsonBackReference 解決嵌套外鏈問題。
@RepositoryRestResourcepublic 配合spring-boot-starter-data-rest使用。
二、注解(annotations)詳解
@SpringBootApplication: 申明讓spring boot自動給程序進(jìn)行必要的配置,這個(gè)配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個(gè)配置。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@ResponseBody: 表示該方法的返回結(jié)果直接寫入HTTP response body中,一般在異步獲取數(shù)據(jù)時(shí)使用,用于構(gòu)建RESTful的api。在使用@RequestMapping后,返回值通常解析為跳轉(zhuǎn)路徑,加上@responsebody后返回結(jié)果不會被解析為跳轉(zhuǎn)路徑,而是直接寫入HTTP response body中。
比如異步獲取json數(shù)據(jù),加上@responsebody后,會直接返回json數(shù)據(jù)。該注解一般會配合@RequestMapping一起使用。
示例代碼:
@RequestMapping(“/test”)
@ResponseBody
public String test(){
return”ok”;
}
@Controller: 用于定義控制器類,在spring 項(xiàng)目中由控制器負(fù)責(zé)將用戶發(fā)來的URL請求轉(zhuǎn)發(fā)到對應(yīng)的服務(wù)接口(service層),一般這個(gè)注解在類中,通常方法需要配合注解@RequestMapping。
示例代碼:
@Controller
@RequestMapping(“/demoInfo”)
publicclass DemoController {
@Autowired
private DemoInfoService demoInfoService;
@RequestMapping("/hello")
public String hello(Map map) {
System.out.println("DemoController.hello()");
map.put("hello","from TemplateController.helloHtml");
//會使用hello.html或者h(yuǎn)ello.ftl模板進(jìn)行渲染顯示.
return"/hello";
}
}
@RestController: 用于標(biāo)注控制層組件(如struts中的action),@ResponseBody和@Controller的合集。
示例代碼:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(“/demoInfo2”)
publicclass DemoController2 {
@RequestMapping("/test")
public String test(){
return"ok";
}
}
@RequestMapping: 提供路由信息,負(fù)責(zé)URL到Controller中的具體函數(shù)的映射。
@EnableAutoConfiguration: Spring Boot自動配置(auto-configuration):嘗試根據(jù)你添加的jar依賴自動配置你的Spring應(yīng)用。例如,如果你的classpath下存在HSQLDB,并且你沒有手動配置任何數(shù)據(jù)庫連接beans,那么我們將自動配置一個(gè)內(nèi)存型(in-memory)數(shù)據(jù)庫”。
你可以將@EnableAutoConfiguration或者@SpringBootApplication注解添加到一個(gè)@Configuration類上來選擇自動配置。如果發(fā)現(xiàn)應(yīng)用了你不想要的特定自動配置類,你可以使用@EnableAutoConfiguration注解的排除屬性來禁用它們。
@ComponentScan: 表示將該類自動發(fā)現(xiàn)掃描組件。個(gè)人理解相當(dāng)于,如果掃描到有@Component、@Controller、@Service等這些注解的類,并注冊為Bean,可以自動收集所有的Spring組件,包括@Configuration類。
我們經(jīng)常使用@ComponentScan注解搜索beans,并結(jié)合@Autowired注解導(dǎo)入。可以自動收集所有的Spring組件,包括@Configuration類。我們經(jīng)常使用@ComponentScan注解搜索beans,并結(jié)合@Autowired注解導(dǎo)入。
如果沒有配置的話,Spring Boot會掃描啟動類所在包下以及子包下的使用了@Service,@Repository等注解的類。
@Configuration: 相當(dāng)于傳統(tǒng)的xml配置文件,如果有些第三方庫需要用到xml文件,建議仍然通過@Configuration類作為項(xiàng)目的配置主類——可以使用@ImportResource注解加載xml配置文件。
@Import: 用來導(dǎo)入其他配置類。
@ImportResource: 用來加載xml配置文件。
@Autowired: 自動導(dǎo)入依賴的bean
@Service: 一般用于修飾service層的組件
@Repository: 使用@Repository注解可以確保DAO或者repositories提供異常轉(zhuǎn)譯,這個(gè)注解修飾的DAO或者repositories類會被ComponetScan發(fā)現(xiàn)并配置,同時(shí)也不需要為它們提供XML配置項(xiàng)。
@Bean: 用@Bean標(biāo)注方法等價(jià)于XML中配置的bean。
@Value: 注入Spring boot application.properties配置的屬性的值。
示例代碼:
@Value(value = “#{message}”)
private String message;
@Inject: 等價(jià)于默認(rèn)的@Autowired,只是沒有required屬性;
@Component: 泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。
@Bean: 相當(dāng)于XML中的,放在方法的上面,而不是類,意思是產(chǎn)生一個(gè)bean,并交給spring管理。
@AutoWired: 自動導(dǎo)入依賴的bean。byType方式。把配置好的Bean拿來用,完成屬性、方法的組裝,它可以對類成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作。當(dāng)加上(required=false)時(shí),就算找不到bean也不報(bào)錯(cuò)。
@Qualifier: 當(dāng)有多個(gè)同一類型的Bean時(shí),可以用@Qualifier(“name”)來指定。與@Autowired配合使用。@Qualifier限定描述符除了能根據(jù)名字進(jìn)行注入,但能進(jìn)行更細(xì)粒度的控制如何選擇候選者,具體使用方式如下:
@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;
@Resource(name=”name”,type=”type”): 沒有括號內(nèi)內(nèi)容的話,默認(rèn)byName。與@Autowired干類似的事。
三、JPA注解
@Entity: @Table(name=”“): 表明這是一個(gè)實(shí)體類。一般用于jpa這兩個(gè)注解一般一塊使用,但是如果表名和實(shí)體類名相同的話,@Table可以省略
@MappedSuperClass: 用在確定是父類的entity上。父類的屬性子類可以繼承。
@NoRepositoryBean: 一般用作父類的repository,有這個(gè)注解,spring不會去實(shí)例化該repository。
@Column: 如果字段名與列名相同,則可以省略。
@Id: 表示該屬性為主鍵。
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = “repair_seq”): 表示主鍵生成策略是sequence(可以為Auto、IDENTITY、native等,Auto表示可在多個(gè)數(shù)據(jù)庫間切換),指定sequence的名字是repair_seq。
@SequenceGeneretor(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize = 1): name為sequence的名稱,以便使用,sequenceName為數(shù)據(jù)庫的sequence名稱,兩個(gè)名稱可以一致。
@Transient: 表示該屬性并非一個(gè)到數(shù)據(jù)庫表的字段的映射,ORM框架將忽略該屬性。如果一個(gè)屬性并非數(shù)據(jù)庫表的字段映射,就務(wù)必將其標(biāo)示為@Transient,否則,ORM框架默認(rèn)其注解為@Basic。@Basic(fetch=FetchType.LAZY):標(biāo)記可以指定實(shí)體屬性的加載方式
@JsonIgnore: 作用是json序列化時(shí)將Java bean中的一些屬性忽略掉,序列化和反序列化都受影響。
@JoinColumn(name=”loginId”): 一對一:本表中指向另一個(gè)表的外鍵。一對多:另一個(gè)表指向本表的外鍵。
@OneToOne、@OneToMany、@ManyToOne: 對應(yīng)hibernate配置文件中的一對一,一對多,多對一。
四、springMVC相關(guān)注解
@RequestMapping: @RequestMapping(“/path”)表示該控制器處理所有“/path”的UR L請求。RequestMapping是一個(gè)用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。該注解有六個(gè)屬性:
params:指定request中必須包含某些參數(shù)值是,才讓該方法處理。 headers:指定request中必須包含某些指定的header值,才能讓該方法處理請求。 value:指定請求的實(shí)際地址,指定的地址可以是URI Template 模式 method:指定請求的method類型, GET、POST、PUT、DELETE等 consumes:指定處理請求的提交內(nèi)容類型(Content-Type),如application/json,text/html; produces:指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型中包含該指定類型才返回
@RequestParam: 用在方法的參數(shù)前面。
@RequestParam String a =request.getParameter(“a”)。
@PathVariable: 路徑變量。如
RequestMapping(“user/get/mac/{macAddress}”)
public String getByMacAddress(@PathVariable String macAddress){
//do something;
}
參數(shù)與大括號里的名字一樣要相同。
五、全局異常處理
@ControllerAdvice: 包含@Component。可以被掃描到。統(tǒng)一處理異常。
@ExceptionHandler(Exception.class): 用在方法上面表示遇到這個(gè)異常就執(zhí)行以下方法。
【墻裂推薦】
最近熱門內(nèi)容回顧? ?#技術(shù)人系列

往期推薦
下方二維碼關(guān)注我

互聯(lián)網(wǎng)草根,堅(jiān)持分享技術(shù)、創(chuàng)業(yè)、產(chǎn)品等心得和總結(jié)~

點(diǎn)擊“閱讀原文”,領(lǐng)取 2020 年最新免費(fèi)技術(shù)資料大全
