SpringBoot各種注解詳解
一、注解列表
@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan讓spring Boot掃描到Configuration類并把它加入到程序上下文。
@Configuration等同于spring的XML配置文件;使用Java代碼可以檢查類型安全。
@EnableAutoConfiguration自動配置。
@ComponentScan組件掃描,可自動發(fā)現(xiàn)和裝配一些Bean。
@Component可配合CommandLineRunner使用,在程序啟動后執(zhí)行一些基礎任務。
@RestController注解是@Controller和@ResponseBody的合集,表示這是個控制器bean,并且是將函數(shù)的返回值直 接填入HTTP響應體中,是REST風格的控制器。
@Autowired自動導入。
@PathVariable獲取參數(shù)。
@JsonBackReference解決嵌套外鏈問題。
@RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。
二、注解詳解
@SpringBootApplication:申明讓spring boot自動給程序進行必要的配置,這個配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個配置。
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:表示該方法的返回結果直接寫入HTTP response body中,一般在異步獲取數(shù)據(jù)時使用,用于構建RESTful的api。在使用@RequestMapping后,返回值通常解析為跳轉路徑,加上@responsebody后返回結果不會被解析為跳轉路徑,而是直接寫入HTTP response body中。
比如異步獲取json數(shù)據(jù),加上@responsebody后,會直接返回json數(shù)據(jù)。該注解一般會配合@RequestMapping一起使用。
示例代碼:
@RequestMapping(“/test”) @ResponseBody public String test(){ return”ok”; }
@Controller:用于定義控制器類,在spring 項目中由控制器負責將用戶發(fā)來的URL請求轉發(fā)到對應的服務接口(service層),一般這個注解在類中,通常方法需要配合注解@RequestMapping。
示例代碼:
@Controller @RequestMapping(“/demoInfo”) publicclass DemoController { @Autowired private DemoInfoService demoInfoService; @RequestMapping("/hello") public String hello(Map<String,Object> map){ System.out.println("DemoController.hello()"); map.put("hello","from TemplateController.helloHtml"); //會使用hello.html或者hello.ftl模板進行渲染顯示. return"/hello"; } }
@RestController:用于標注控制層組件(如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:提供路由信息,負責URL到Controller中的具體函數(shù)的映射。
@EnableAutoConfiguration:Spring Boot自動配置(auto-configuration):嘗試根據(jù)你添加的jar依賴自動配置你的Spring應用。例如,如果你的classpath下存在HSQLDB,并且你沒有手動配置任何數(shù)據(jù)庫連接beans,那么我們將自動配置一個內存型(in-memory)數(shù)據(jù)庫”。
你可以將@EnableAutoConfiguration或者@SpringBootApplication注解添加到一個@Configuration類上來選擇自動配置。如果發(fā)現(xiàn)應用了你不想要的特定自動配置類,你可以使用@EnableAutoConfiguration注解的排除屬性來禁用它們。
@ComponentScan:表示將該類自動發(fā)現(xiàn)掃描組件。個人理解相當于,如果掃描到有@Component、@Controller、@Service等這些注解的類,并注冊為Bean,可以自動收集所有的Spring組件,包括@Configuration類。
我們經常使用@ComponentScan注解搜索beans,并結合@Autowired注解導入。可以自動收集所有的Spring組件,包括@Configuration類。我們經常使用@ComponentScan注解搜索beans,并結合@Autowired注解導入。
如果沒有配置的話,Spring Boot會掃描啟動類所在包下以及子包下的使用了@Service,@Repository等注解的類。
@Configuration:相當于傳統(tǒng)的xml配置文件,如果有些第三方庫需要用到xml文件,建議仍然通過@Configuration類作為項目的配置主類——可以使用@ImportResource注解加載xml配置文件。
@Import:用來導入其他配置類。
@ImportResource:用來加載xml配置文件。
@Autowired:自動導入依賴的bean
@Service:一般用于修飾service層的組件
@Repository:使用@Repository注解可以確保DAO或者repositories提供異常轉譯,這個注解修飾的DAO或者repositories類會被ComponetScan發(fā)現(xiàn)并配置,同時也不需要為它們提供XML配置項。
@Bean:用@Bean標注方法等價于XML中配置的bean。
@Value:注入Spring boot application.properties配置的屬性的值。
示例代碼:
@Value(value = “#{message}”) private String message;
@Inject:等價于默認的@Autowired,只是沒有required屬性;
@Component:泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。
@Bean:相當于XML中的,放在方法的上面,而不是類,意思是產生一個bean,并交給spring管理。
@AutoWired:自動導入依賴的bean。byType方式。把配置好的Bean拿來用,完成屬性、方法的組裝,它可以對類成員變量、方法及構造函數(shù)進行標注,完成自動裝配的工作。當加上(required=false)時,就算找不到bean也不報錯。
@Qualifier:當有多個同一類型的Bean時,可以用@Qualifier(“name”)來指定。與@Autowired配合使用。@Qualifier限定描述符除了能根據(jù)名字進行注入,但能進行更細粒度的控制如何選擇候選者,具體使用方式如下:
@Autowired @Qualifier(value = “demoInfoService”) private DemoInfoService demoInfoService;
@Resource(name=”name”,type=”type”):沒有括號內內容的話,默認byName。與@Autowired干類似的事。
到此這篇關于SpringBoot各種注解詳解的文章就介紹到這了,更多相關SpringBoot注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
HashMap實現(xiàn)保存兩個key相同的數(shù)據(jù)
這篇文章主要介紹了HashMap實現(xiàn)保存兩個key相同的數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot使用thymeleaf實現(xiàn)一個前端表格方法詳解
Thymeleaf是一個現(xiàn)代的服務器端 Java 模板引擎,適用于 Web 和獨立環(huán)境。Thymeleaf 的主要目標是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板,本文就來用它實現(xiàn)一個前端表格,感興趣的可以了解一下2022-10-10java實現(xiàn)圖片轉base64字符串 java實現(xiàn)base64字符串轉圖片
這篇文章主要為大家詳細介紹了java實現(xiàn)圖片轉base64字符串,java實現(xiàn)base64字符串轉圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02詳談spring中bean注入無效和new創(chuàng)建對象的區(qū)別
這篇文章主要介紹了spring中bean注入無效和new創(chuàng)建對象的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Springboot?整合maven插口調用maven?release?plugin實現(xiàn)一鍵打包功能
這篇文章主要介紹了Springboot?整合maven插口調用maven?release?plugin實現(xiàn)一鍵打包功能,整合maven-invoker使程序去執(zhí)行mvn命令,結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-03-03