這篇文章給大家介紹spring4的新特性有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的兩當(dāng)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
從Spring4開(kāi)始,Spring以Servlet3為進(jìn)行開(kāi)發(fā),如果用Spring MVC 測(cè)試框架的話需要指定Servlet3兼容的jar包(因?yàn)槠銶ock的對(duì)象都是基于Servlet3的)。另外為了方便Rest開(kāi)發(fā),通過(guò)新的@RestController指定在控制器上,這樣就不需要在每個(gè)@RequestMapping方法上加 @ResponseBody了。而且添加了一個(gè)AsyncRestTemplate ,支持REST客戶端的異步無(wú)阻塞支持。
1、@RestController
Java代碼
@RestController public class UserController { private UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @RequestMapping("/test") public User view() { User user = new User(); user.setId(1L); user.setName("haha"); return user; } @RequestMapping("/test2") public String view2() { return "{\"id\" : 1}"; } }
其實(shí)現(xiàn)就是在@@RestController中加入@ResponseBody:
Java代碼
@org.springframework.stereotype.Controller @org.springframework.web.bind.annotation.ResponseBod public @interface RestController { }
這樣當(dāng)你開(kāi)發(fā)Rest服務(wù)器端的時(shí)候,spring-mvc配置文件需要的代碼極少,可能就僅需如下一行:
Java代碼
<context:component-scan base-package="com.bjpowernode.spring4"/> <mvc:annotation-driven/>
2、mvc:annotation-driven配置變化
統(tǒng)一風(fēng)格;將 enableMatrixVariables改為enable-matrix-variables屬性;將ignoreDefaultModelOnRedirect改為ignore-default-model-on-redirect。
3、提供AsyncRestTemplate用于客戶端非阻塞異步支持。
3.1、服務(wù)器端
Java代碼
@RestController public class UserController { private UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @RequestMapping("/api") public Callable<User> api() { System.out.println("=====hello"); return new Callable<User>() { @Override public User call() throws Exception { Thread.sleep(10L * 1000); //暫停兩秒 User user = new User(); user.setId(1L); user.setName("haha"); return user; } }; } }
非常簡(jiǎn)單,服務(wù)器端暫停10秒再返回結(jié)果(但是服務(wù)器也是非阻塞的)。具體參考我github上的代碼。
3.2、客戶端
Java代碼
public static void main(String[] args) { AsyncRestTemplate template = new AsyncRestTemplate(); //調(diào)用完后立即返回(沒(méi)有阻塞) ListenableFuture<ResponseEntity<User>> future = template.getForEntity("http://localhost:9080/spring4/api", User.class); //設(shè)置異步回調(diào) future.addCallback(new ListenableFutureCallback<ResponseEntity<User>>() { @Override public void onSuccess(ResponseEntity<User> result) { System.out.println("======client get result : " + result.getBody()); } @Override public void onFailure(Throwable t) { System.out.println("======client failure : " + t); } }); System.out.println("==no wait"); }
此處使用Future來(lái)完成非阻塞,這樣的話我們也需要給它一個(gè)回調(diào)接口來(lái)拿結(jié)果; Future和Callable是一對(duì),一個(gè)消費(fèi)結(jié)果,一個(gè)產(chǎn)生結(jié)果。調(diào)用完模板后會(huì)立即返回,不會(huì)阻塞;有結(jié)果時(shí)會(huì)調(diào)用其回調(diào)。
AsyncRestTemplate默認(rèn)使用SimpleClientHttpRequestFactory,即通過(guò)java.net.HttpURLConnection實(shí)現(xiàn);另外我們也可以使用apache的http components;使用template.setAsyncRequestFactory(new HttpComponentsAsyncClientHttpRequestFactory());設(shè)置即可。
另外在開(kāi)發(fā)時(shí)盡量不要自己注冊(cè)如:
Java代碼
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
盡量使用
Java代碼
<mvc:annotation-driven/>
它設(shè)計(jì)的已經(jīng)足夠好,使用子元素可以配置我們需要的配置。
且不要使用老版本的:
Java代碼
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
否則可能得到如下異常:
寫道
Circular view path [login]: would dispatch back to the current handler URL [/spring4/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
關(guān)于spring4的新特性有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
本文題目:spring4的新特性有哪些
網(wǎng)址分享:http://chinadenli.net/article2/ihoeic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、域名注冊(cè)、商城網(wǎng)站、App開(kāi)發(fā)、App設(shè)計(jì)、標(biāo)簽優(yōu)化
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)