1、使用注解需要匯入的依賴
-
1、1在application.xml檔案中加入該約束
xmlns:context=http://www.springframework.org/schema/context
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
?并且需要加入標簽開啟該注解
<context:annotation-config/>
或指定要掃描的包,包下的注解就會生效
<context:component-scan base-package="com.kuang.pojo"/>
?最終xml代碼
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
-
1、2對應注解含義
@Autowired 自動裝配,優先匹配型別,后名字
@Qualifier(value = "https://www.cnblogs.com/nhgtx/archive/2022/11/16/xxx")可指定自動裝配的id
@Resource(name = "xxx") 自動裝配,優先名字,后型別,也可指定名稱
@Nullable 該注解后的引數可為空
@Component 組件,放在類上,說明該類被spring管理了,就是bean
mvc三層架構:
dao:@Repository
service:@Service
Controller:@Controller
功能一樣,都是將該類注冊到spring中,裝配bean
該注解需配合<context:component-scan base-package="com.kuang.dao"/>掃包進行使用
任需ClassPathXmlApplicationContext,無法脫離組態檔
@Value("xxx")該注解用于給屬性進行注入,也能夠直接注入與set方法中
@Scope("xxx")該注解指定對應類的作用域,是單例模式或原型模式或其它
lombok包下的快速生成物體類物件的注解{
@NoArgsConstructor快速創建無參構造
@AllArgsConstructor 快速創建有參構造
@Data 快速創建get,set
}
??spring4之后要使用注解必須匯入aop包,若發現注解無效,可查看是否匯入該包
使用java配置spring,完全舍棄spring的xml組態檔
@Configuration:將類指定為spring配置類
@Bean:指定該方法為xml中相當于<bean> 需回傳一個物體類
@ComponentScan("xxxx"):使該配置類只掃描到指定的包下
@Import({xxx.class}):合并多個配置類
SpingMVC注解開發
@RequestMapping("/xxx"):該注解可映射一個訪問路徑,在單個方法上時直接訪問 http://localhost:8080/xxx
在類上時訪問需加上類的訪問路徑 http://localhost:8080/類上的映射名/xxx
在回傳單純的資料時,它可以進行亂碼決議
@RequestMapping(value = "https://www.cnblogs.com/sda",produces = "application/json;charset=utf-8")
RestFul風格
@PathVariable
加在引數前,可定義為路徑變數
未使用前
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulTest {
@RequestMapping("restful")
public String restful(int a, int b, Model model){
int c = a+b;
model.addAttribute("msg",c);
return "hello";
}
}

使用后
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulTest {
@RequestMapping("/restful/{a}/{b}")
public String restful(@PathVariable int a, @PathVariable int b, Model model){
int c = a+b;
model.addAttribute("msg",c);
return "hello";
}
}

restful是一種風格,并非規范或標準
restful指定訪問方式
@RequestMapping(value
value可換成path,禁止使用name,會出問題
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RestfulTest {
@RequestMapping(value = "https://www.cnblogs.com/restful/{a}/{b}",method = RequestMethod.GET)
public String restful(@PathVariable int a, @PathVariable int b, Model model){
int c = a+b;
model.addAttribute("msg",c);
return "hello";
}
}
通過在注解中選擇method可以指定通過什么方式來進行訪問該路徑才能得到對應的方法,
通過另外的注解也能實作對應的效果
@RequestMapping(name = "/restful/{a}/{b}",method = RequestMethod.GET)
//get方法可以用
@GetMapping("xxx")
//相同的,也有DeleteMapping等對應的注解可以實作method = RequestMethod.xxx
使用GetMapping注解接收前端引數,可直接從引數中獲取,也可使用注解指定引數名
@GetMapping("/t1")
public ModelAndView he(@RequestParam("hs")String hs,User user){
System.out.println(user);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg",user+"\n"+hs);
modelAndView.setViewName("hello");
return modelAndView;
}
@RequestParam("xxx") 指定該引數接收時的引數名必須為xxx
@Param("xxx")也可給指定引數一個別名
向前端回傳資料,繞過視圖決議器
在方法上寫上@ResponseBody添加該注解,則繞過視圖決議器,僅回傳資料,不跳轉視圖
在類上添加@RestController注解,該類下的所有方法都只會回傳資料,不跳轉視圖
Qualifier
@Qualifier
當bean中存在多個BookService型別物件時,搭配@Qualifier(“實作類名稱”)表明注入的是哪一個具體實作類的bean(與 @Autowired配合使用加以區分)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535168.html
標籤:其他
上一篇:【k哥爬蟲普法】非法入侵計算機資訊系統,獲取1500萬余條個人資訊!
下一篇:Http和Https的區別?
