一、啟動注解 @SpringBootApplication@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFi...
@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@interfaceSpringBootApplication{
}
查看源碼可發(fā)現(xiàn), @SpringBootApplication
是一個復(fù)合注解,包含了 @SpringBootConfiguration
, @EnableAutoConfiguration`,
@ComponentScan` 這三個注解
`@SpringBootConfiguration
注解,繼承 @Configuration
注解,主要用于加載配置文件 @SpringBootConfiguration
繼承自 @Configuration
,二者功能也一致,標(biāo)注當(dāng)前類是配置類, 并會將當(dāng)前類內(nèi)聲明的一個或多個以 @Bean 注解標(biāo)記的方法的實(shí)例納入到 spring 容器中,并且實(shí)例名就是方法名。
@EnableAutoConfiguration
注解,開啟自動配置功能 @EnableAutoConfiguration
可以幫助 SpringBoot 應(yīng)用將所有符合條件的 @Configuration
配置都加載到當(dāng)前 SpringBoot 創(chuàng)建并使用的 IoC 容器。借助于 Spring 框架原有的一個工具類:SpringFactoriesLoader 的支持, @EnableAutoConfiguration
可以智能的自動配置功效才得以大功告成
@ComponentScan
注解,主要用于組件掃描和自動裝配 @ComponentScan
的功能其實(shí)就是自動掃描并加載符合條件的組件或 bean 定義,最終將這些 bean 定義加載到容器中。我們可以通過 basePackages 等屬性指定 @ComponentScan
自動掃描的范圍,如果不指定,則默認(rèn) Spring 框架實(shí)現(xiàn)從聲明 @ComponentScan
所在類的 package 進(jìn)行掃描,默認(rèn)情況下是不指定的,所以 SpringBoot 的啟動類最好放在 root package 下。
控制器,處理 http 請求。
查看 @RestController 源碼
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public@interfaceRestController{
@AliasFor(annotation = Controller.class)
String value() default"";
}
從源碼我們知道, @RestController
注解相當(dāng)于 @ResponseBody
+ @Controller
合在一起的作用, RestController 使用的效果是將方法返回的對象直接在瀏覽器上展示成 json 格式.
通過 HttpMessageConverter 讀取 Request Body 并反序列化為 Object(泛指)對象
@RequestMapping 是 Spring Web 應(yīng)用程序中最常被用到的注解之一。這個注解會將 HTTP 請求映射到 MVC 和 REST 控制器的處理方法上
注解簡寫:@RequestMapping(value = "/say",method = RequestMethod.GET) 等價于:@GetMapping(value = "/say")
GetMapping 源碼
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public@interfaceGetMapping{
}
是 @RequestMapping(method = RequestMethod.GET) 的縮寫
@PostMapping 用于將 HTTP post 請求映射到特定處理程序的方法注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public@interfacePostMapping{
}
是 @RequestMapping(method = RequestMethod.POST) 的縮寫
@PathVariable: 獲取 url 中的數(shù)據(jù)
@Controller
@RequestMapping("/User")
publicclassHelloWorldController{
@RequestMapping("/getUser/{uid}")
publicString getUser(@PathVariable("uid")Integer id, Model model) {
System.out.println("id:"+id);
return"user";
}
}
請求示例:http://localhost:8080/User/getUser/123
@Controller
@RequestMapping("/User")
publicclassHelloWorldController{
@RequestMapping("/getUser")
publicString getUser(@RequestParam("uid")Integer id, Model model) {
System.out.println("id:"+id);
return"user";
}
}
請求示例:http://localhost:8080/User/getUser?uid=123
@Repository DAO 層注解,DAO 層中接口繼承 JpaRepository, 需要在 build.gradle 中引入相關(guān) jpa 的一個 jar 自動加載。
Repository 注解源碼
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public@interfaceRepository{
@AliasFor(annotation = Component.class)
String value() default"";
}
@Service
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public@interfaceService{
@AliasFor(annotation = Component.class)
String value() default"";
}
@Service 是 @Component 注解的一個特例,作用在類上 @Service 注解作用域默認(rèn)為單例 使用注解配置和類路徑掃描時,被 @Service 注解標(biāo)注的類會被 Spring 掃描并注冊為 Bean @Service 用于標(biāo)注服務(wù)層組件, 表示定義一個 bean @Service 使用時沒有傳參數(shù),Bean 名稱默認(rèn)為當(dāng)前類的類名,首字母小寫 @Service(“serviceBeanId”) 或 @Service(value=”serviceBeanId”) 使用時傳參數(shù),使用 value 作為 Bean 名字 @Scope 作用域注解 @Scope 作用在類上和方法上,用來配置 spring bean 的作用域,它標(biāo)識 bean 的作用域
@Scope 源碼
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public@interfaceScope{
@AliasFor("scopeName")
String value() default"";
@AliasFor("value")
String scopeName() default"";
ScopedProxyMode proxyMode() defaultScopedProxyMode.DEFAULT;
}
value
singleton 表示該 bean 是單例的。(默認(rèn))
prototype 表示該 bean 是多例的,即每次使用該 bean 時都會新建一個對象。
request 在一次 http 請求中,一個 bean 對應(yīng)一個實(shí)例。
session 在一個 httpSession 中,一個 bean 對應(yīng)一個實(shí)例。
proxyMode
DEFAULT 不使用代理。(默認(rèn))
NO 不使用代理,等價于 DEFAULT。
INTERFACES 使用基于接口的代理 (jdk dynamic proxy)。
TARGET_CLASS 使用基于類的代理 (cglib)。
@Entity 實(shí)體類注解
@Table(name ="數(shù)據(jù)庫表名"),這個注解也注釋在實(shí)體類上,對應(yīng)數(shù)據(jù)庫中相應(yīng)的表。
@Id、@Column 注解用于標(biāo)注實(shí)體類中的字段,pk 字段標(biāo)注為 @Id,其余 @Column。
@Bean 產(chǎn)生一個 bean 的方法
@Bean 明確地指示了一種方法,產(chǎn)生一個 bean 的方法,并且交給 Spring 容器管理。支持別名 @Bean("xx-name")
@Autowired 自動導(dǎo)入
@Autowired 注解作用在構(gòu)造函數(shù)、方法、方法參數(shù)、類字段以及注解上
@Autowired 注解可以實(shí)現(xiàn) Bean 的自動注入
@Component 把普通 pojo 實(shí)例化到 spring 容器中,相當(dāng)于配置文件中的
雖然有了 @Autowired, 但是我們還是要寫一堆 bean 的配置文件, 相當(dāng)麻煩, 而 @Component 就是告訴 spring, 我是 pojo 類, 把我注冊到容器中吧, spring 會自動提取相關(guān)信息。那么我們就不用寫麻煩的 xml 配置文件了
引入單個 properties 文件:
@PropertySource(value = {"classpath : xxxx/xxx.properties"})
引入多個 properties 文件:
@PropertySource(value = {"classpath : xxxx/xxx.properties","classpath : xxxx.properties"})
可以額外分為兩種模式 相對路徑 classpath,絕對路徑(真實(shí)路徑)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("${properties 中的鍵}") private String xxx;
功能類似 XML 配置的,用來導(dǎo)入配置類,可以導(dǎo)入帶有 @Configuration 注解的配置類或?qū)崿F(xiàn)了 ImportSelector/ImportBeanDefinitionRegistrar。
使用示例
@SpringBootApplication@Import({SmsConfig.class})publicclassDemoApplication{publicstaticvoid main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
在 Spring 中,事務(wù)有兩種實(shí)現(xiàn)方式,分別是編程式事務(wù)管理和聲明式事務(wù)管理兩種方式
編程式事務(wù)管理:編程式事務(wù)管理使用 TransactionTemplate 或者直接使用底層的 PlatformTransactionManager。對于編程式事務(wù)管理,spring 推薦使用 TransactionTemplate。聲明式事務(wù)管理:建立在 AOP 之上的。其本質(zhì)是對方法前后進(jìn)行攔截,然后在目標(biāo)方法開始之前創(chuàng)建或者加入一個事務(wù),在執(zhí)行完目標(biāo)方法之后根據(jù)執(zhí)行情況提交或者回滾事務(wù),通過 @Transactional 就可以進(jìn)行事務(wù)操作,更快捷而且簡單。推薦使用
@ControllerAdvice 統(tǒng)一處理異常
@ControllerAdvice 注解定義全局異常處理類
@ControllerAdvice
publicclassGlobalExceptionHandler{
}
@ExceptionHandler注解聲明異常處理方法
@ControllerAdvice
publicclassGlobalExceptionHandler{
@ExceptionHandler(Exception.class)
@ResponseBody
String handleException(){
return"Exception Deal!";
}
}
來源:本文內(nèi)容搜集或轉(zhuǎn)自各大網(wǎng)絡(luò)平臺,并已注明來源、出處,如果轉(zhuǎn)載侵犯您的版權(quán)或非授權(quán)發(fā)布,請聯(lián)系小編,我們會及時審核處理。
聲明:江蘇教育黃頁對文中觀點(diǎn)保持中立,對所包含內(nèi)容的準(zhǔn)確性、可靠性或者完整性不提供任何明示或暗示的保證,不對文章觀點(diǎn)負(fù)責(zé),僅作分享之用,文章版權(quán)及插圖屬于原作者。
Copyright©2013-2025 ?JSedu114 All Rights Reserved. 江蘇教育信息綜合發(fā)布查詢平臺保留所有權(quán)利
蘇公網(wǎng)安備32010402000125
蘇ICP備14051488號-3技術(shù)支持:南京博盛藍(lán)睿網(wǎng)絡(luò)科技有限公司
南京思必達(dá)教育科技有限公司版權(quán)所有 百度統(tǒng)計