【原標(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