純手寫SpringBoot框架之註解方式啟動SpringMVC容器
阿新 • • 發佈:2019-06-25
使用Java語言建立Tomcat容器,並且通過Tomcat執行Servlet,接下來,將會使用Java語言在SpringBoot建立內建Tomcat,使用註解方式啟動SpringMVC容器。
程式碼實現。
1.pom.xml檔案,需要依賴的jar包。
<dependencies> <!--Java語言操作Tomcat--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.23</version> </dependency> <!--tomcat對jsp的支援--> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper</artifactId> <version>7.0.47</version> </dependency> <!--spring-web--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.8.RELEASE</version> </dependency> <!--spring-webmvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.8.RELEASE</version> </dependency> </dependencies>
2.配置SpringMVC、Spring、DispatcherServlet的初始化類:
/** * 載入springmvc====dispatcherservlet */ public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { //載入根配置資訊 spring核心 protected Class<?>[] getRootConfigClasses() { return new Class[0]; } //springmvc 載入配置資訊 protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } //springmvc攔截的url對映,攔截所有請求 protected String[] getServletMappings() { return new String[]{"/"};//攔截所有請求 } }
AbstractAnnotationConfigDispatcherServletInitializer這個類負責"初始化Spring容器、SpringMVC容器、配置DispatcherServlet"。
- getRootConfigClasses()方法用於獲取Spring應用容器的配置檔案,這裡我們給定預先定義的RootConfig.class;
- getServletConfigClasses負責獲取Spring MVC應用容器,這裡傳入預先定義好的WebConfig.class;
- getServletMappings()方法負責指定需要由DispatcherServlet對映的路徑,這裡給定的是"/",意思是由DispatcherServlet處理所有向該應用發起的請求。
SpringMVC配置,載入SpringMVC容器。這裡的@Configuration相當於springmvc.xml配置檔案;@ComponentScan(basePackages = "com.jiuyue.controller")相當於配置檔案sringmvc.xml的掃描controler應該還有印象吧;@EnableWebMvc則是開啟SpringMVC功能。
/**
* SpringMVC配置資訊
*/
@Configuration //相當於配置檔案springmvc.xml
@EnableWebMvc //開啟SpringMVC功能
@ComponentScan(basePackages = "com.jiuyue.controller")//掃springMVC的
public class WebConfig extends WebMvcConfigurerAdapter {
//配置檢視轉換器
}
根配置,載入Spring容器。
/**
* 根配置
*/
@Configuration
@ComponentScan(basePackages = "com.jiuyue")//掃整個spring專案的
public class RootConfig {
}
3.啟動類
public class APP {
public static void main(String[] args) throws ServletException, LifecycleException {
//使用Java內建tomcat執行SpringMVC框架
//原理:tomcat載入到SpringMVC註解啟動方式,就會建立SpringMVC容器
start();
}
public static void start() throws LifecycleException, ServletException {
//建立tomcat伺服器
Tomcat tomcatServer = new Tomcat();
//設定port
tomcatServer.setPort(8085);
//讀取專案路徑,'/'可以載入靜態資源
StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").getAbsolutePath());
//禁止重新載入
ctx.setReloadable(false);
//class檔案讀取地址
File addtionWebInfoClasses = new File("target/classes");
//建立WebRoot
WebResourceRoot resources = new StandardRoot(ctx);
//tomcat內部讀取class檔案進行執行
//內部虛擬Tomcat空間
resources.addPreResources(new DirResourceSet(resources,"/WEB-INF/classes",addtionWebInfoClasses.getAbsolutePath(),"/"));
tomcatServer.start();
System.out.println("Java語言建立Tomcat啟動成功");
//非同步進行接收請求
tomcatServer.getServer().await();
}
}
controller類
@Controller
public class IndexController {
@RequestMapping(value = "/index",produces = "text/html;charset=UTF-8")
@ResponseBody
public String index(){
return "純手寫Java語言實現SpringBoot註解啟動SpringMVC容器";
}
}
訪問效果圖
配置檢視解析器
直接下WebConfig配置類中配置就可以,WebConfig就相當於springmvc.xml配置檔案,前面我們在裡面配置了掃包與開啟springMVC功能,接下來就在下面繼續配置檢視解析器。
@Configuration //相當與配置檔案
@EnableWebMvc //開啟SpringMVC功能
@ComponentScan(basePackages = "com.jiuyue.controller")//掃springMVC的
public class WebConfig extends WebMvcConfigurerAdapter {
//配置檢視轉換器
//SpringMVC檢視解析器
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
//可以在JSP頁面中通過${}訪問bean
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
}
}
JspController控制器類
@Controller
public class JspController {
@RequestMapping(value = "/jspIndex")
public String jspIndex(){
return "jspIndex";
}
}
WEB-INF/jsp目錄
訪問jspIndex返回jsp頁面
業務邏輯層
將根配置,配置到初始化類
/**
* 根配置
*/
@Configuration
@ComponentScan(basePackages = "com.jiuyue")//掃整個spring專案的
public class RootConfig {
}
public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
//載入根配置資訊 spring核心
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
//springmvc 載入配置資訊
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
//springmvc攔截的url對映,攔截所有請求
protected String[] getServletMappings() {
return new String[]{"/"};//攔截所有請求
}
}
Service類
@Service
public class IndexService {
public String index(){
return "純手寫Java語言實現SpringBoot註解啟動SpringMVC容器";
}
}
控制器類
如果文章有錯的地方歡迎指正,大家互相交