xmlns:context=http://www.springframework.org/schema/context
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
?并且需要加入標(biāo)簽開啟該注解
成都創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!成都創(chuàng)新互聯(lián)具備承接各種類型的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作項(xiàng)目的能力。經(jīng)過十多年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。
<context:annotation-config/>
或指定要掃描的包,包下的注解就會(huì)生效
<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>
@Autowired 自動(dòng)裝配,優(yōu)先匹配類型,后名字
@Qualifier(value = "xxx")可指定自動(dòng)裝配的id
@Resource(name = "xxx") 自動(dòng)裝配,優(yōu)先名字,后類型,也可指定名稱
@Nullable 該注解后的參數(shù)可為空
@Component 組件,放在類上,說明該類被spring管理了,就是bean
mvc三層架構(gòu):
dao:@Repository
service:@Service
Controller:@Controller
功能一樣,都是將該類注冊(cè)到spring中,裝配bean
該注解需配合<context:component-scan base-package="com.kuang.dao"/>掃包進(jìn)行使用
任需ClassPathXmlApplicationContext,無法脫離配置文件
@Value("xxx")該注解用于給屬性進(jìn)行注入,也能夠直接注入與set方法中
@Scope("xxx")該注解指定對(duì)應(yīng)類的作用域,是單例模式或原型模式或其它
lombok包下的快速生成實(shí)體類對(duì)象的注解{
@NoArgsConstructor快速創(chuàng)建無參構(gòu)造
@AllArgsConstructor 快速創(chuàng)建有參構(gòu)造
@Data 快速創(chuàng)建get,set
}
??spring4之后要使用注解必須導(dǎo)入aop包,若發(fā)現(xiàn)注解無效,可查看是否導(dǎo)入該包
@Configuration:將類指定為spring配置類
@Bean:指定該方法為xml中相當(dāng)于<bean> 需返回一個(gè)實(shí)體類
@ComponentScan("xxxx"):使該配置類只掃描到指定的包下
@Import({xxx.class}):合并多個(gè)配置類
@RequestMapping("/xxx"):該注解可映射一個(gè)訪問路徑,在單個(gè)方法上時(shí)直接訪問 http://localhost:8080/xxx
在類上時(shí)訪問需加上類的訪問路徑 http://localhost:8080/類上的映射名/xxx
在返回單純的數(shù)據(jù)時(shí),它可以進(jìn)行亂碼解析
@RequestMapping(value = "/sda",produces = "application/json;charset=utf-8")
@PathVariable
加在參數(shù)前,可定義為路徑變量
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}/")
public String restful(@PathVariable int a, @PathVariable int b, Model model){
int c = a+b;
model.addAttribute("msg",c);
return "hello";
}
}
@RequestMapping(value
value可換成path,禁止使用name,會(huì)出問題
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 = "/restful/{a}/",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可以指定通過什么方式來進(jìn)行訪問該路徑才能得到對(duì)應(yīng)的方法。
@RequestMapping(name = "/restful/{a}/",method = RequestMethod.GET)
//get方法可以用
@GetMapping("xxx")
//相同的,也有DeleteMapping等對(duì)應(yīng)的注解可以實(shí)現(xiàn)method = RequestMethod.xxx
@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") 指定該參數(shù)接收時(shí)的參數(shù)名必須為xxx
@Param("xxx")也可給指定參數(shù)一個(gè)別名
在方法上寫上@ResponseBody添加該注解,則繞過視圖解析器,僅返回?cái)?shù)據(jù),不跳轉(zhuǎn)視圖
在類上添加@RestController注解,該類下的所有方法都只會(huì)返回?cái)?shù)據(jù),不跳轉(zhuǎn)視圖
Qualifier
當(dāng)bean中存在多個(gè)BookService類型對(duì)象時(shí),搭配@Qualifier(“實(shí)現(xiàn)類名稱”)表明注入的是哪一個(gè)具體實(shí)現(xiàn)類的bean(與 @Autowired配合使用加以區(qū)分)
標(biāo)題名稱:Spring注解開發(fā)
本文網(wǎng)址:http://jinyejixie.com/article16/dsdihgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、定制網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站營銷、商城網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)