国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Spring Boot 這么火,常用注解和原理都給你整理好了

作者:云天 

來源:http://39sd.cn/5E6F1

一、啟動(dòng)注解 @SpringBootApplication

@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 {
    // ... 此處省略源碼
}

查看源碼可發(fā)現(xiàn),@SpringBootApplication是一個(gè)復(fù)合注解,包含了@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan這三個(gè)注解

@SpringBootConfiguration 注解,繼承@Configuration注解,主要用于加載配置文件
@SpringBootConfiguration繼承自@Configuration,二者功能也一致,標(biāo)注當(dāng)前類是配置類, 并會(huì)將當(dāng)前類內(nèi)聲明的一個(gè)或多個(gè)以@Bean注解標(biāo)記的方法的實(shí)例納入到spring容器中,并且實(shí)例名就是方法名。

@EnableAutoConfiguration 注解,開啟自動(dòng)配置功能
@EnableAutoConfiguration可以幫助SpringBoot應(yīng)用將所有符合條件的@Configuration配置都加載到當(dāng)前SpringBoot創(chuàng)建并使用的IoC容器。借助于Spring框架原有的一個(gè)工具類:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自動(dòng)配置功效才得以大功告成

@ComponentScan 注解,主要用于組件掃描和自動(dòng)裝配
@ComponentScan的功能其實(shí)就是自動(dòng)掃描并加載符合條件的組件或bean定義,最終將這些bean定義加載到容器中。我們可以通過basePackages等屬性指定@ComponentScan自動(dòng)掃描的范圍,如果不指定,則默認(rèn)Spring框架實(shí)現(xiàn)從聲明@ComponentScan所在類的package進(jìn)行掃描,默認(rèn)情況下是不指定的,所以SpringBoot的啟動(dòng)類最好放在root package下。

二、Controller 相關(guān)注解

@Controller
控制器,處理http請(qǐng)求。

@RestController 復(fù)合注解
查看@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注解相當(dāng)于@ResponseBody+@Controller合在一起的作用,RestController使用的效果是將方法返回的對(duì)象直接在瀏覽器上展示成json格式.

@RequestBody
通過HttpMessageConverter讀取Request Body并反序列化為Object(泛指)對(duì)象

@RequestMapping
@RequestMapping 是 Spring Web 應(yīng)用程序中最常被用到的注解之一。這個(gè)注解會(huì)將 HTTP 請(qǐng)求映射到 MVC 和 REST 控制器的處理方法上

@GetMapping用于將HTTP get請(qǐng)求映射到特定處理程序的方法注解
注解簡寫:@RequestMapping(value = '/say',method = RequestMethod.GET)等價(jià)于:@GetMapping(value = '/say')

GetMapping源碼

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {
//...
}

是@RequestMapping(method = RequestMethod.GET)的縮寫

@PostMapping用于將HTTP post請(qǐng)求映射到特定處理程序的方法注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
    //...
}

是@RequestMapping(method = RequestMethod.POST)的縮寫

三、取請(qǐng)求參數(shù)值

@PathVariable:獲取url中的數(shù)據(jù)

@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';
}
}

請(qǐng)求示例:http://localhost:8080/User/getUser/123

@RequestParam:獲取請(qǐng)求參數(shù)的值

@Controller
@RequestMapping('/User')
public class HelloWorldController {

@RequestMapping('/getUser')
public String getUser(@RequestParam('uid')Integer id, Model model) {
    System.out.println('id:'+id);
    return 'user';
}
}

請(qǐng)求示例:http://localhost:8080/User/getUser?uid=123

@RequestHeader 把Request請(qǐng)求header部分的值綁定到方法的參數(shù)上
@CookieValue 把Request header中關(guān)于cookie的值綁定到方法的參數(shù)上

四、注入bean相關(guān)

@Repository
DAO層注解,DAO層中接口繼承JpaRepository,需要在build.gradle中引入相關(guān)jpa的一個(gè)jar自動(dòng)加載。

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 '';

}
@Service
@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 '';
}

@Service是@Component注解的一個(gè)特例,作用在類上
@Service注解作用域默認(rèn)為單例
使用注解配置和類路徑掃描時(shí),被@Service注解標(biāo)注的類會(huì)被Spring掃描并注冊(cè)為Bean
@Service用于標(biāo)注服務(wù)層組件,表示定義一個(gè)bean
@Service使用時(shí)沒有傳參數(shù),Bean名稱默認(rèn)為當(dāng)前類的類名,首字母小寫
@Service(“serviceBeanId”)或@Service(value=”serviceBeanId”)使用時(shí)傳參數(shù),使用value作為Bean名字
@Scope作用域注解
@Scope作用在類上和方法上,用來配置 spring bean 的作用域,它標(biāo)識(shí) 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是單例的。(默認(rèn))
   prototype   表示該bean是多例的,即每次使用該bean時(shí)都會(huì)新建一個(gè)對(duì)象。
   request     在一次http請(qǐng)求中,一個(gè)bean對(duì)應(yīng)一個(gè)實(shí)例。
   session     在一個(gè)httpSession中,一個(gè)bean對(duì)應(yīng)一個(gè)實(shí)例。

proxyMode
   DEFAULT         不使用代理。(默認(rèn))
   NO              不使用代理,等價(jià)于DEFAULT。
   INTERFACES      使用基于接口的代理(jdk dynamic proxy)。
   TARGET_CLASS    使用基于類的代理(cglib)。

@Entity實(shí)體類注解
@Table(name ='數(shù)據(jù)庫表名'),這個(gè)注解也注釋在實(shí)體類上,對(duì)應(yīng)數(shù)據(jù)庫中相應(yīng)的表。
@Id、@Column注解用于標(biāo)注實(shí)體類中的字段,pk字段標(biāo)注為@Id,其余@Column。

@Bean產(chǎn)生一個(gè)bean的方法
@Bean明確地指示了一種方法,產(chǎn)生一個(gè)bean的方法,并且交給Spring容器管理。支持別名@Bean('xx-name')

@Autowired 自動(dòng)導(dǎo)入
@Autowired注解作用在構(gòu)造函數(shù)、方法、方法參數(shù)、類字段以及注解上
@Autowired注解可以實(shí)現(xiàn)Bean的自動(dòng)注入
@Component
把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的

雖然有了@Autowired,但是我們還是要寫一堆bean的配置文件,相當(dāng)麻煩,而@Component就是告訴spring,我是pojo類,把我注冊(cè)到容器中吧,spring會(huì)自動(dòng)提取相關(guān)信息。那么我們就不用寫麻煩的xml配置文件了

五、導(dǎo)入配置文件

@PropertySource注解

引入單個(gè)properties文件:

@PropertySource(value = {'classpath : xxxx/xxx.properties'})

引入多個(gè)properties文件:

@PropertySource(value = {'classpath : xxxx/xxx.properties','classpath : xxxx.properties'})

@ImportResource導(dǎo)入xml配置文件

可以額外分為兩種模式 相對(duì)路徑classpath,絕對(duì)路徑(真實(shí)路徑)file

注意:單文件可以不寫value或locations,value和locations都可用

相對(duì)路徑(classpath)

引入單個(gè)xml配置文件:@ImportSource('classpath : xxx/xxxx.xml')

引入多個(gè)xml配置文件:@ImportSource(locations={'classpath : xxxx.xml' , 'classpath : yyyy.xml'})

絕對(duì)路徑(file)

引入單個(gè)xml配置文件:@ImportSource(locations= {'file : d:/hellxz/dubbo.xml'})

引入多個(gè)xml配置文件:@ImportSource(locations= {'file : d:/hellxz/application.xml' , 'file : d:/hellxz/dubbo.xml'})

取值:使用@Value注解取配置文件中的值

@Value('${properties中的鍵}')
private String xxx;

@Import 導(dǎo)入額外的配置信息
功能類似XML配置的,用來導(dǎo)入配置類,可以導(dǎo)入帶有@Configuration注解的配置類或?qū)崿F(xiàn)了ImportSelector/ImportBeanDefinitionRegistrar。

使用示例

@SpringBootApplication
@Import({SmsConfig.class})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

六、事務(wù)注解 @Transactional

在Spring中,事務(wù)有兩種實(shí)現(xiàn)方式,分別是編程式事務(wù)管理和聲明式事務(wù)管理兩種方式

編程式事務(wù)管理:編程式事務(wù)管理使用TransactionTemplate或者直接使用底層的PlatformTransactionManager。對(duì)于編程式事務(wù)管理,spring推薦使用TransactionTemplate。
聲明式事務(wù)管理:建立在AOP之上的。其本質(zhì)是對(duì)方法前后進(jìn)行攔截,然后在目標(biāo)方法開始之前創(chuàng)建或者加入一個(gè)事務(wù),在執(zhí)行完目標(biāo)方法之后根據(jù)執(zhí)行情況提交或者回滾事務(wù),通過@Transactional就可以進(jìn)行事務(wù)操作,更快捷而且簡單。推薦使用

七、全局異常處理

@ControllerAdvice 統(tǒng)一處理異常
@ControllerAdvice 注解定義全局異常處理類

@ControllerAdvice
public class GlobalExceptionHandler {
}

@ExceptionHandler 注解聲明異常處理方法

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    String handleException(){
        return 'Exception Deal!';
    }
}

END



本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SpringBoot-常見問題(一)
Spring Boot 注解大全,真是太全了!
Springboot 常用注解
Spring常用注解(收藏大全)
SpringBoot須掌握的幾個(gè)注解
Spring中component-scan標(biāo)簽的use-default-filters屬性的作用以及...
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服