@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { // ... 此處省略源碼 }
查看源碼可發現,@SpringBootApplication是一個複合註解,包含了@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan這三個註解java
@SpringBootConfiguration繼承自@Configuration,兩者功能也一致,標註當前類是配置類, 並會將當前類內聲明的一個或多個以@Bean註解標記的方法的實例歸入到spring容器中,而且實例名就是方法名。git
@EnableAutoConfiguration能夠幫助SpringBoot應用將全部符合條件的@Configuration配置都加載到當前SpringBoot建立並使用的IoC容器。藉助於Spring框架原有的一個工具類:SpringFactoriesLoader的支持,@EnableAutoConfiguration能夠智能的自動配置功效才得以大功告成github
@ComponentScan的功能其實就是自動掃描並加載符合條件的組件或bean定義,最終將這些bean定義加載到容器中。咱們能夠經過basePackages等屬性指定@ComponentScan自動掃描的範圍,若是不指定,則默認Spring框架實現從聲明@ComponentScan所在類的package進行掃描,默認狀況下是不指定的,因此SpringBoot的啓動類最好放在root package下。spring
控制器,處理http請求。數據庫
查看@RestController源碼編程
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty String otherwise) * @since 4.0.1 */ @AliasFor(annotation = Controller.class) String value() default ""; }
從源碼咱們知道,@RestController註解至關於@ResponseBody+@Controller合在一塊兒的做用,RestController使用的效果是將方法返回的對象直接在瀏覽器上展現成json格式.json
經過HttpMessageConverter讀取Request Body並反序列化爲Object(泛指)對象瀏覽器
@RequestMapping 是 Spring Web 應用程序中最常被用到的註解之一。這個註解會將 HTTP 請求映射到 MVC 和 REST 控制器的處理方法上springboot
註解簡寫:@RequestMapping(value = "/say",method = RequestMethod.GET)等價於:@GetMapping(value = "/say")cookie
GetMapping源碼
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @RequestMapping(method = RequestMethod.GET) public @interface GetMapping { //... }
是@RequestMapping(method = RequestMethod.GET)的縮寫
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @RequestMapping(method = RequestMethod.POST) public @interface PostMapping { //... }
是@RequestMapping(method = RequestMethod.POST)的縮寫
@Controller @RequestMapping("/User") public class HelloWorldController { @RequestMapping("/getUser/{uid}") public String getUser(@PathVariable("uid")Integer id, Model model) { System.out.println("id:"+id); return "user"; } }
請求示例:http://localhost:8080/User/getUser/123
@Controller
@RequestMapping("/User")
public class HelloWorldController {
@RequestMapping("/getUser") public String getUser(@RequestParam("uid")Integer id, Model model) { System.out.println("id:"+id); return "user"; }
}
請求示例:http://localhost:8080/User/getUser?uid=123
DAO層註解,DAO層中接口繼承JpaRepository<T,ID extends Serializable>,須要在build.gradle中引入相關jpa的一個jar自動加載。
Repository註解源碼
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Repository { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty String otherwise) */ @AliasFor(annotation = Component.class) String value() default ""; }
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty String otherwise) */ @AliasFor(annotation = Component.class) String value() default ""; }
@Scope做用在類上和方法上,用來配置 spring bean 的做用域,它標識 bean 的做用域
@Scope源碼
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Scope { /** * Alias for {@link #scopeName}. * @see #scopeName */ @AliasFor("scopeName") String value() default ""; @AliasFor("value") String scopeName() default ""; ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT; }
屬性介紹
value singleton 表示該bean是單例的。(默認) prototype 表示該bean是多例的,即每次使用該bean時都會新建一個對象。 request 在一次http請求中,一個bean對應一個實例。 session 在一個httpSession中,一個bean對應一個實例。 proxyMode DEFAULT 不使用代理。(默認) NO 不使用代理,等價於DEFAULT。 INTERFACES 使用基於接口的代理(jdk dynamic proxy)。 TARGET_CLASS 使用基於類的代理(cglib)。
@Table(name ="數據庫表名"),這個註解也註釋在實體類上,對應數據庫中相應的表。
@Id、@Column註解用於標註實體類中的字段,pk字段標註爲@Id,其他@Column。
@Bean明確地指示了一種方法,產生一個bean的方法,而且交給Spring容器管理。支持別名@Bean("xx-name")
把普通pojo實例化到spring容器中,至關於配置文件中的
雖然有了@Autowired,可是咱們仍是要寫一堆bean的配置文件,至關麻煩,而@Component就是告訴spring,我是pojo類,把我註冊到容器中吧,spring會自動提取相關信息。那麼咱們就不用寫麻煩的xml配置文件了
引入單個properties文件:
@PropertySource(value = {"classpath : xxxx/xxx.properties"})
引入多個properties文件:
@PropertySource(value = {"classpath : xxxx/xxx.properties","classpath : xxxx.properties"})
能夠額外分爲兩種模式 相對路徑classpath,絕對路徑(真實路徑)file
注意:單文件能夠不寫value或locations,value和locations均可用
相對路徑(classpath)
引入單個xml配置文件:@ImportSource("classpath : xxx/xxxx.xml")
引入多個xml配置文件:@ImportSource(locations={"classpath : xxxx.xml" , "classpath : yyyy.xml"})
絕對路徑(file)
引入單個xml配置文件:@ImportSource(locations= {"file : d:/hellxz/dubbo.xml"})
引入多個xml配置文件:@ImportSource(locations= {"file : d:/hellxz/application.xml" , "file : d:/hellxz/dubbo.xml"})
取值:使用@Value註解取配置文件中的值
@Value("${properties中的鍵}")
private String xxx;
功能相似XML配置的,用來導入配置類,能夠導入帶有@Configuration註解的配置類或實現了ImportSelector/ImportBeanDefinitionRegistrar。
使用示例
@SpringBootApplication @Import({SmsConfig.class}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
在Spring中,事務有兩種實現方式,分別是編程式事務管理和聲明式事務管理兩種方式
@ControllerAdvice 註解定義全局異常處理類
@ControllerAdvice public class GlobalExceptionHandler { }
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) @ResponseBody String handleException(){ return "Exception Deal!"; } }