類注解
@component 標(biāo)注類,泛指各種組件,類不屬于各種分類的時(shí)候,用它做標(biāo)注。
@Service 標(biāo)注類,聲明該類為業(yè)務(wù)層組件,用于處理業(yè)務(wù)邏輯
@Repositor 標(biāo)注類,聲明該類為持久層的接口。使用后,在啟動(dòng)主程序類上需要添加@MapperScan(“xxx.xxx.xxx.mapper”)注解
@Mapper 標(biāo)注類,用在持久層的接口上,注解使用后相當(dāng)于@Reponsitory加@MapperScan注解,會(huì)自動(dòng)進(jìn)行配置加載
@Configuration Spring3.0以上,聲明該類是一個(gè)配置類,可以使用@Configuration用于定義配置類,可替換xml配置文件。被注解的類內(nèi)部包含有一個(gè)或多個(gè)被@Bean注解的方法。
@Aspect 標(biāo)注類 聲明這個(gè)類是一個(gè)切面類
@Controller 標(biāo)注類,聲明該類為Spring MVC controller處理器組件,用于創(chuàng)建處理http請(qǐng)求的對(duì)象。
@RestController 標(biāo)注類,聲明該類為Rest風(fēng)格控制器組件,該注解是Spring4之后加入的注解,用它替代@Controller就不需要再配置@ResponseBody,默認(rèn)返回json格式
@RequestMapping:既可以注解在類上,也可以注解在類的方法上,該類提供初步的請(qǐng)求映射信息。注解在類上是相對(duì)于 Web 根目錄,注解在方法上的是相對(duì)于類上的路徑
1
2
3
4
5
6
7
|
@Controller @RequestMapping ( "/user" ) public class UserController { @RequestMapping ( "/login" ) public String login() { return "success" ; } |
此時(shí),調(diào)用時(shí)使用:http://IP地址:端口號(hào)/網(wǎng)站根路徑/user/login
方法或?qū)傩陨献⒔?/h2>
@Autowired 用來裝配bean,可以寫在字段或者方法上。默認(rèn)情況下必須要求依賴對(duì)象必須存在,如果要允許null值,可以設(shè)置它的required屬性為false,如:@Autowired(required=false)
@Qualifier 如果一個(gè)接口有兩個(gè)或者兩個(gè)以上的實(shí)現(xiàn)類,就要使用到@Qualifier注解,qualifier的英文含義是合格者的意思,通過此注解,標(biāo)注那個(gè)實(shí)現(xiàn)類才是這次要用到的實(shí)現(xiàn)類。如:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Service ( "service" ) public class EmployeeServiceImpl implements EmployeeService { public EmployeeDto getEmployeeById(Long id) { return new EmployeeDto(); } } @Service ( "service1" ) public class EmployeeServiceImpl1 implements EmployeeService { public EmployeeDto getEmployeeById(Long id) { return new EmployeeDto(); } } |
service和service1同時(shí)實(shí)現(xiàn)接口EmployeeService,@Autowired注入時(shí),通過@Qualifier告訴spring,要哪一個(gè)實(shí)現(xiàn)類,代碼如下
1
2
3
|
@Autowired @Qualifier ( "service" ) EmployeeService employeeService; |
此處是service,而不是service1。
@Bean 與@Configuration標(biāo)注類配合使用,等同于xml文件配置的bean。如:
1
2
3
4
|
< bean id = "user" class = "com.zhang.bean.User" > < property name = "userName" value = "zhangsan" ></ property > < property name = "age" value = "26" ></ property > </ bean > |
等同于
1
2
3
4
5
6
7
|
@Bean public User getUser(){ User user = new User(); user.setUserName( "zhangsan" ), user.setAge( 26 ), return user; } |
@After、@Before、@Around:與@Aspect配合使用,直接將切點(diǎn)作為參數(shù),在方法執(zhí)行之后執(zhí)行、之前執(zhí)行及之前和之后均執(zhí)行。
@RequestBody:可用在方法上,也可以用在參數(shù)上。注解在方法上,代表用戶返回json數(shù)據(jù),而不是頁面。
參數(shù)注解
@RequestBody:注解在方法的參數(shù)上,代表接收的參數(shù)是來自requestBody中,即請(qǐng)求體。用于處理非 Content-Type: application/x-www-form-urlencoded編碼格式的數(shù)據(jù),如:application/json、application/xml等類型的數(shù)據(jù),使用注解@RequestBody可以將body里面所有的json數(shù)據(jù)傳到后端,后端再進(jìn)行解析
@RequestParam:使用在方法參數(shù)參數(shù)上,接收的參數(shù)是來自HTTP請(qǐng)求體或請(qǐng)求url的QueryString中。可以接受簡(jiǎn)單類型的屬性,也可以接受對(duì)象類型。@RequestParam用來處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內(nèi)容,Content-Type默認(rèn)為該屬性。
@PathVariable: 使用在方法參數(shù)參數(shù)上。當(dāng)@RequestMapping URI template 樣式映射時(shí), paramId可通過 @Pathvariable注解綁定它傳過來的值到方法的參數(shù)上,如:
1
2
3
4
5
6
7
8
|
@Controller @RequestMapping ( "/user/{Id}" ) public class DemoController { @RequestMapping ( "/pets/{petId}" ) public void queryPetByParam( @PathVariable String Id, @PathVariable String petId) { // implementation } } |
以上就是Spring框架學(xué)習(xí)常用注解匯總的詳細(xì)內(nèi)容,更多關(guān)于Spring框架注解的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/guoyp2126/article/details/110221840