女人久久久,最近更新中文字幕在线,成人国内精品久久久久影院vr,中文字幕亚洲综合久久综合,久久精品秘?一区二区三区美小说

原創(chuàng)生活

國(guó)內(nèi) 商業(yè) 滾動(dòng)

基金 金融 股票

期貨金融

科技 行業(yè) 房產(chǎn)

銀行 公司 消費(fèi)

生活滾動(dòng)

保險(xiǎn) 海外 觀察

財(cái)經(jīng) 生活 期貨

當(dāng)前位置:滾動(dòng) >

springmvc請(qǐng)求過(guò)程分析 SpringBoot框架之注解方式啟動(dòng)

文章來(lái)源:財(cái)金網(wǎng)  發(fā)布時(shí)間: 2019-04-11 09:57:15  責(zé)任編輯:cfenews.com
+|-

【原標(biāo)題:springmvc請(qǐng)求過(guò)程分析 SpringBoot框架之注解方式啟動(dòng)】上一篇,使用Java語(yǔ)言創(chuàng)建Tomcat容器,并且通過(guò)Tomcat執(zhí)行Servlet,接下來(lái),將會(huì)使用Java語(yǔ)言在SpringBoot創(chuàng)建內(nèi)置Tomcat,使用注解方式啟動(dòng)SpringMVC容器。

代碼實(shí)現(xiàn)。

1.pom.xml文件,需要依賴的jar包。

org.apache.tomcat.embed

tomcat-embed-core

8.5.23

org.apache.tomcat

tomcat-jasper

7.0.47

org.springframework

spring-web

5.0.8.RELEASE

org.springframework

spring-webmvc

5.0.8.RELEASE

2.配置SpringMVC、Spring、DispatcherServlet的初始化類:

/**

*加載springmvc====dispatcherservlet

*/

publicclassSpringWebAppInitializerextendsAbstractAnnotationConfigDispatcherServletInitializer{

//加載根配置信息spring核心

protectedClass[]getRootConfigClasses(){

returnnewClass[0];

}

//springmvc加載配置信息

protectedClass[]getServletConfigClasses(){

returnnewClass[]{WebConfig.class};

}

//springmvc攔截的url映射,攔截所有請(qǐng)求

protectedString[]getServletMappings(){

returnnewString[]{"/"};//攔截所有請(qǐng)求

}

}

AbstractAnnotationConfigDispatcherServletInitializer這個(gè)類負(fù)責(zé)"初始化Spring容器、SpringMVC容器、配置DispatcherServlet"。

getRootConfigClasses()方法用于獲取Spring應(yīng)用容器的配置文件,這里我們給定預(yù)先定義的RootConfig.class;

getServletConfigClasses負(fù)責(zé)獲取Spring MVC應(yīng)用容器,這里傳入預(yù)先定義好的WebConfig.class;

getServletMappings()方法負(fù)責(zé)指定需要由DispatcherServlet映射的路徑,這里給定的是"/",意思是由DispatcherServlet處理所有向該應(yīng)用發(fā)起的請(qǐng)求。

SpringMVC配置,加載SpringMVC容器。這里的@Configuration相當(dāng)于springmvc.xml配置文件;@ComponentScan(basePackages = "com.jiuyue.controller")相當(dāng)于配置文件sringmvc.xml的掃描controler應(yīng)該還有印象吧;@EnableWebMvc則是開(kāi)啟SpringMVC功能。

/**

*SpringMVC配置信息

*/

@Configuration//相當(dāng)于配置文件springmvc.xml

@EnableWebMvc//開(kāi)啟SpringMVC功能

@ComponentScan(basePackages="com.jiuyue.controller")//掃springMVC的

publicclassWebConfigextendsWebMvcConfigurerAdapter{

//配置視圖轉(zhuǎn)換器

}

根配置,加載Spring容器。

/**

*根配置

*/

@Configuration

@ComponentScan(basePackages="com.jiuyue")//掃整個(gè)spring項(xiàng)目的

publicclassRootConfig{

}

3.啟動(dòng)類

publicclassAPP{

publicstaticvoidmain(String[]args)throwsServletException,LifecycleException{

//使用Java內(nèi)置tomcat運(yùn)行SpringMVC框架

//原理:tomcat加載到SpringMVC注解啟動(dòng)方式,就會(huì)創(chuàng)建SpringMVC容器

start();

}

publicstaticvoidstart()throwsLifecycleException,ServletException{

//創(chuàng)建tomcat服務(wù)器

TomcattomcatServer=newTomcat();

//設(shè)置port

tomcatServer.setPort(8085);

//讀取項(xiàng)目路徑,'/'可以加載靜態(tài)資源

StandardContextctx=(StandardContext)tomcatServer.addWebapp("/",newFile("src/main").getAbsolutePath());

//禁止重新載入

ctx.setReloadable(false);

//class文件讀取地址

FileaddtionWebInfoClasses=newFile("target/classes");

//創(chuàng)建WebRoot

WebResourceRootresources=newStandardRoot(ctx);

//tomcat內(nèi)部讀取class文件進(jìn)行執(zhí)行

//內(nèi)部虛擬Tomcat空間

resources.addPreResources(newDirResourceSet(resources,"/WEB-INF/classes",addtionWebInfoClasses.getAbsolutePath(),"/"));

tomcatServer.start();

System.out.println("Java語(yǔ)言創(chuàng)建Tomcat啟動(dòng)成功");

//異步進(jìn)行接收請(qǐng)求

tomcatServer.getServer().await();

}

}

controller類

@Controller

publicclassIndexController{

@RequestMapping(value="/index",produces="text/html;charset=UTF-8")

@ResponseBody

publicStringindex(){

return"純手寫(xiě)Java語(yǔ)言實(shí)現(xiàn)SpringBoot注解啟動(dòng)SpringMVC容器";

}

}

訪問(wèn)效果圖

配置視圖解析器

直接下WebConfig配置類中配置就可以,WebConfig就相當(dāng)于springmvc.xml配置文件,前面我們?cè)诶锩媾渲昧藪甙c開(kāi)啟springMVC功能,接下來(lái)就在下面繼續(xù)配置視圖解析器。

@Configuration//相當(dāng)與配置文件

@EnableWebMvc//開(kāi)啟SpringMVC功能

@ComponentScan(basePackages="com.jiuyue.controller")//掃springMVC的

publicclassWebConfigextendsWebMvcConfigurerAdapter{

//配置視圖轉(zhuǎn)換器

//SpringMVC視圖解析器

@Bean

publicViewResolverviewResolver(){

InternalResourceViewResolverviewResolver=newInternalResourceViewResolver();

viewResolver.setPrefix("/WEB-INF/jsp/");

viewResolver.setSuffix(".jsp");

//可以在JSP頁(yè)面中通過(guò)${}訪問(wèn)bean

viewResolver.setExposeContextBeansAsAttributes(true);

returnviewResolver;

}

}

JspController控制器類

@Controller

publicclassJspController{

@RequestMapping(value="/jspIndex")

publicStringjspIndex(){

return"jspIndex";

}

}

WEB-INF/jsp目錄

訪問(wèn)jspIndex返回jsp頁(yè)面

業(yè)務(wù)邏輯層

將根配置,配置到初始化類

/**

*根配置

*/

@Configuration

@ComponentScan(basePackages="com.jiuyue")//掃整個(gè)spring項(xiàng)目的

publicclassRootConfig{

}

publicclassSpringWebAppInitializerextendsAbstractAnnotationConfigDispatcherServletInitializer{

//加載根配置信息spring核心

protectedClass[]getRootConfigClasses(){

returnnewClass[]{RootConfig.class};

}

//springmvc加載配置信息

protectedClass[]getServletConfigClasses(){

returnnewClass[]{WebConfig.class};

}

//springmvc攔截的url映射,攔截所有請(qǐng)求

protectedString[]getServletMappings(){

returnnewString[]{"/"};//攔截所有請(qǐng)求

}

}

Service類

@Service

publicclassIndexService{

publicStringindex(){

return"純手寫(xiě)Java語(yǔ)言實(shí)現(xiàn)SpringBoot注解啟動(dòng)SpringMVC容器";

}

}

控制器類

@ResponseBody

@RequestMapping(value="/index2",produces="text/html;charset=UTF-8")

publicStringindex2(){

returnindexService.index();

}

如果文章有錯(cuò)的地方歡迎指正,大家互相交流。習(xí)慣在微信看技術(shù)文章的同學(xué),可以關(guān)注微信公眾號(hào):SeptemberNotes

專題首頁(yè)|財(cái)金網(wǎng)首頁(yè)

原創(chuàng)
新聞

精彩
互動(dòng)

獨(dú)家
觀察

京ICP備2021034106號(hào)-38   營(yíng)業(yè)執(zhí)照公示信息  財(cái)金網(wǎng)  版權(quán)所有  cfenews.com  投稿郵箱:362293157@qq.com  業(yè)務(wù)QQ:362293157立即發(fā)帖