注解說(shuō)明

注解說(shuō)明
-@Autowired 自動(dòng)裝配通過(guò)類型。名字
如果@Autowired不能唯一自動(dòng)裝配,通過(guò)@Qualifier(value = “xxx”)
-- @Nullable 說(shuō)明該字段可以為空
-@Resource 自動(dòng)裝配通過(guò)名字。類型

--@Component 組件 //相當(dāng)于<bean id="user" class="com.ma.pojo.User"></bean>
放在類上,說(shuō)明這個(gè)類被spring管理了,就是bean

-- @Value("xxx") 給參數(shù)賦值
//相當(dāng)于<property name="name" value="xxx"/> 也可以用在set方法上  

pojo--@Component
dao—@Repository
service—@Service
controller-- @Controller

    在javaconfig中用
@Configuration  // @Configuration代表這是一個(gè)配置類,與applicationContext.xml一樣

@ComponentScan(basePackages = "com.ma") //使用啟用組件掃描
@Import(xxxconfig.class) //導(dǎo)入其他配置
@Bean //注冊(cè)一個(gè)bean,就相當(dāng)于bean標(biāo)簽
@Scope 注解的目的是用來(lái)調(diào)節(jié)作用域
@Scope("prototype")//多實(shí)例,IOC容器啟動(dòng)創(chuàng)建的時(shí)候,并不會(huì)創(chuàng)建對(duì)象放在容器在容器當(dāng)中,當(dāng)你需要的時(shí)候,需要從容器當(dāng)中取該對(duì)象的時(shí)候,就會(huì)創(chuàng)建。
@Scope("singleton")//單實(shí)例 IOC容器啟動(dòng)的時(shí)候就會(huì)調(diào)用方法創(chuàng)建對(duì)象,以后每次獲取都是從容器當(dāng)中拿同一個(gè)對(duì)象(map當(dāng)中)。
@Scope("request")//同一個(gè)請(qǐng)求創(chuàng)建一個(gè)實(shí)例
@Scope("session")//同一個(gè)session創(chuàng)建一個(gè)實(shí)例

Springmvc

注解

@RequestParam請(qǐng)求接口時(shí),URL是:http://www.test.com/user/getUserById?userId=1

//@RequestParam用法,注意這里請(qǐng)求后面沒(méi)有添加參數(shù)

@RequestMapping(value = "/test",method = RequestMethod.POST)

public Result test(@RequestParam(value="id",required=false,defaultValue="0")String id)

注意上面@RequestParam用法當(dāng)中的參數(shù)。

<pre style="background:white">@PathVariable("xxx")</pre>

<pre style="margin-left:21.0pt;mso-para-margin-left:2.0gd;background:white">通過(guò) @PathVariable 可以將URL中占位符參數(shù){xxx}綁定到處理器類的方法形參中@PathVariable(“xxx“) </pre>

<pre style="margin-left:21.0pt;mso-para-margin-left:2.0gd;background:white">@RequestMapping(“user/{id}/{name}”)</pre>

<pre style="margin-left:21.0pt;mso-para-margin-left:2.0gd;background:white">請(qǐng)求路徑:http://localhost:8080/user/1/james</pre>

@PathVariable主要用于接收http://host:port/path/{參數(shù)值}數(shù)據(jù)。@RequestParam主要用于接收http://host:port/path?參數(shù)名=參數(shù)值數(shù)據(jù),這里后面也可以不跟參數(shù)值。

@Controller 控制層

@RequestMapping("/xxx/{xxx}")請(qǐng)求

Post請(qǐng)求:@RequestMapping(value="/test",method = RequestMethod.POST)

RequestMapping中有六個(gè)屬性分別是:

value: 指定請(qǐng)求的實(shí)際地址,指定的地址可以是URI Template 模式;

method: 指定請(qǐng)求的method類型, GET、POST、PUT、DELETE等;

consumes: 指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type),例如application/json, text/html;

produces: 指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)類型中包含該指定類型才返回;

params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理。

headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求。
@ResponseBody 不會(huì)通過(guò)視圖解析器 直接傳遞一個(gè)字符串
就可以理解成將java的對(duì)象轉(zhuǎn)換成json字符串的格式給前端解析
@RequestBody 來(lái)處理請(qǐng)求的json格式數(shù)據(jù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容