Spring MVC 系列(二) 基於註解的開發

2021-08-14 23:59:56 字數 1244 閱讀 6254

春季自2.0版本開始,陸續引入了一些註解用於簡化spring的開發

本文就將介紹spring mvc常用註解以及使用場景。

@controller - >

控制層

@service - >業務層

@repository - >道層

@component - >僅僅表示乙個元件(bean),可以作用在任何層次。

spring mvc作為spring框架的web mvc產品,常用的註解當然必不可少的@controller

springmvc-servlet的配置檔案中,對控制器宣告時的名稱屬性

例如:http:// localhost:8091 / springmvc / hello。做

3,

如圖1所示,首先新增註解相關罐

2,在用springmvc-servlet.xml中中配置,指定註解使用範圍

[html]

檢視純

文字

<! - mvc的註解驅動 - >

<

mvc:annotation-driven

/>

<

context:component-scan

base-package

="cn.itcast.springmvc"

/>

<

bean

class

="org.springframework.web.servlet.view.internalresourceviewresolver"

>

<

property

name

="prefix"

value

="/web-inf/jsp/"

>

property

>

<

property

name

="suffix"

value

=".jsp"

>

property

>

bean

>

3、在controller中使用註解

[html]view plain

copy

@controller//用來標註當前類是springmvc的控制層的類  

public class testcontroller   

Spring AOP的基於AspectJ註解開發

1.編寫目標類並配置 這個目標類並沒有實現介面,因此spring會自動用cglib來對其進行動態 public class orderdao void delete void update void find 2.編寫切面類並配置 切面類 public class myaspect 3.使用註解對目...

Java框架(十四)之springMVC的註解開發

1.配置springmvc配置檔案 對映器和介面卡的註解版 mvc annotation driven 2.配置web.xml檔案 springmvc org.springframework.web.servlet.dispatcherservlet contextconfiglocation cl...

基於SpringMVC的RESTful API設計

token驗證 考慮到安全性,除了token認證的api 外,其他所有的請求都需要經過token驗證,springmvc提供的 modelattribute tokenidvalidateresult 註解能夠預先對所有方法進token有效性的驗證,一旦token過期或者token驗證失敗,則不允許...