springboot註解一

一、註解 (annotations) 列表\n@SpringBootApplication:\n包含了 @ComponentScan、@Configuration 和 @EnableAutoConfiguration 註解。\n其中 @ComponentScan 讓 spring Boot 掃描到 Configuration 類並把它加入到程序上下文。\n@Configuration等同於 spring 的 XML 配置文件;使用 Java 代碼可以檢查類型安全。\n**@EnableAutoConfiguration ** 自動配置。\n**@ComponentScan ** 組件掃描,可自動發現和裝配一些 Bean。\n@Component可配合 CommandLineRunner 使用,在程序啓動後執行一些基礎任務。\n@RestController註解是 @Controller 和 @ResponseBody 的合集, 表示這是個控制器 bean, 並且是將函數的返回值直 接填入 HTTP 響應體中, 是 REST 風格的控制器。\n@Autowired自動導入。\n@PathVariable獲取參數。\n@JsonBackReference解決嵌套外鏈問題。\n@RepositoryRestResourcepublic配合 spring-boot-starter-data-rest 使用。\n二、註解 (annotations) 詳解\n@SpringBootApplication:申明讓 spring boot 自動給程序進行必要的配置,這個配置等同於:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個配置。\npackagecom.example.myproject;\nimportorg.springframework.boot.SpringApplication;\nimportorg.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication//sameas@Configuration@EnableAutoConfiguration@ComponentScan\npublicclassApplication{\npublicstaticvoidmain(String[]args){\nSpringApplication.run(Application.class,args);\n}\n}\n@ResponseBody:表示該方法的返回結果直接寫入 HTTP response body 中,一般在異步獲取數據時使用,用於構建 RESTful 的 api。\n在使用 @RequestMapping 後,返回值通常解析爲跳轉路徑,加上 @responsebody 後返回結果不會被解析爲跳轉路徑,而是直接寫入 HTTP response body 中。\n比如異步獲取 json 數據,加上 @responsebody 後,會直接返回 json 數據。\n該註解一般會配合 @RequestMapping 一起使用。示例代碼:\n@RequestMapping(“/test”)\n@ResponseBody\npublicStringtest(){\nreturn”ok”;\n}\n@Controller:用於定義控制器類,在 spring 項目中由控制器負責將用戶發來的 URL 請求轉發到對應的服務接口(service 層)\n一般這個註解在類中,通常方法需要配合註解 @RequestMapping。\n示例代碼:\n@Controller\n@RequestMapping(“/demoInfo”)\npublicclassDemoController{\n@Autowired\nprivateDemoInfoServicedemoInfoService;\n\n@RequestMapping(\"/hello\")\npublicStringhello(Mapmap){\nSystem.out.println(\"DemoController.hello()\");\nmap.put(\"hello\",\"fromTemplateController.helloHtml\");\n//會使用hello.html或者hello.ftl模板進行渲染顯示.\nreturn\"/hello\";\n}\n}\n@RestController:用於標註控制層組件 (如 struts 中的 action),@ResponseBody 和 @Controller 的合集。\n示例代碼:\npackagecom.kfit.demo.web;\n\nimportorg.springframework.web.bind.annotation.RequestMapping;\nimportorg.springframework.web.bind.annotation.RestController;\n\n@RestController\n@RequestMapping(“/demoInfo2”)\npublicclassDemoController2{\n\n@RequestMapping(\"/test\")\npublicStringtest(){\nreturn\"ok\";\n}\n}\n@RequestMapping:提供路由信息,負責 URL 到 Controller 中的具體函數的映射。\n@EnableAutoConfiguration:Spring Boot 自動配置(auto-configuration):嘗試根據你添加的 jar 依賴自動配置你的 Spring 應用。\n例如,如果你的 classpath 下存在 HSQLDB,並且你沒有手動配置任何數據庫連接 beans,那麼我們將自動配置一個內存型(in-memory)數據庫”。\n你可以將 @EnableAutoConfiguration 或者 @SpringBootApplication 註解添加到一個 @Configuration 類上來選擇自動配置。\n搜索公縱號:MarkerHub,關注回覆[vue]獲取前後端入門教程!\n如果發現應用了你不想要的特定自動配置類,你可以使用 @EnableAutoConfiguration 註解的排除屬性來禁用它們。\n@ComponentScan:表示將該類自動發現掃描組件。\n個人理解相當於,如果掃描到有 @Component、@Controller、@Service 等這些註解的類,並註冊爲 Bean,可以自動收集所有的 Spring 組件,包括 @Configuration 類。\n我們經常使用 @ComponentScan 註解搜索 beans,並結合 @Autowired 註解導入。可以自動收集所有的 Spring 組件,包括 @Configuration 類。\n如果沒有配置的話,Spring Boot 會掃描啓動類所在包下以及子包下的使用了 @Service,@Repository 等註解的類。\n@Configuration:相當於傳統的 xml 配置文件,如果有些第三方庫需要用到 xml 文件,建議仍然通過 @Configuration 類作爲項目的配置主類——可以使用 @ImportResource 註解加載 xml 配置文件。\n@Import:用來導入其他配置類。\n@ImportResource:用來加載 xml 配置文件。\n@Autowired:自動導入依賴的 bean\n@Service:一般用於修飾 service 層的組件\n@Repository:使用 @Repository 註解可以確保 DAO 或者 repositories 提供異常轉譯,這個註解修飾的 DAO 或者 repositories 類會被 ComponetScan 發現並配置,同時也不需要爲它們提供 XML 配置項。\n@Bean:用 @Bean 標註方法等價於 XML 中配置的 bean。\n@Value:注入 Spring boot application.properties 配置的屬性的值。示例代碼:\n@Value(value=“#{message}”)\nprivateStringmessage;\n@Inject:等價於默認的 @Autowired,只是沒有 required 屬性;\n@Component:泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。\n@Bean:相當於 XML 中的, 放在方法的上面,而不是類,意思是產生一個 bean, 並交給 spring 管理。\n@AutoWired:自動導入依賴的 bean。byType 方式。把配置好的 Bean 拿來用,完成屬性、方法的組裝,它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作。當加上(required=false)時,就算找不到 bean 也不報錯。\n@Qualifier:當有多個同一類型的 Bean 時,可以用 @Qualifier(“name”) 來指定。與 @Autowired 配合使用。@Qualifier 限定描述符除了能根據名字進行注入,但能進行更細粒度的控制如何選擇候選者,具體使用方式如下:\n@Autowired\n@Qualifier(value=“demoInfoService”)\nprivateDemoInfoServicedemoInfoService;\n@Resource(name=”name”,type=”type”):沒有括號內內容的話,默認 byName。與 @Autowired 幹類似的事。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章