1.基于SpringMVC无配置文件(纯Java)完全注解化+内置tomcat-embed-core实现SpringBoot框架,Main函数启动。
2.SpringBoot核心快速整合第三方框架原理:Maven继承依赖关系
3.SpringBoot内嵌入tomcat-embed-core
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-coreartifactId>
<version>8.5.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.0.4.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.0.4.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.apache.tomcatgroupId>
<artifactId>tomcat-jasperartifactId>
<version>8.5.16version>
dependency>
dependencies>
publicclass IndexServetextends HttpServlet {
@Override
protectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp) throwsServletException, IOException {
doPost(req, resp);
}
@Override
protectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp) throwsServletException, IOException {
resp.getWriter().print("springboot2.0 内置Tomcat的Servlet");
}
}
//端口号
tomcat.getServer().await();
}
DispatcherServlet是Spring MVC的核心,每当应用接受一个HTTP请求,由DispatcherServlet负责将请求分发给应用的其他组件。
在旧版本中,DispatcherServlet之类的servlet一般在web.xml文件中配置,该文件一般会打包进最后的war包种;但是Spring 3引入了注解,我们这节课讲解,如何基于注解配置Spring MVC。
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-coreartifactId>
<version>8.5.16version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.0.4.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.0.4.RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.apache.tomcatgroupId>
<artifactId>tomcat-jasperartifactId>
<version>8.5.16version>
dependency>
dependencies>
package com.rollin.config;
}
package com.rollin.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*
* @author rollin
* web根容器
*
*/
@Configuration
@ComponentScan(basePackages = "com.rollin")
public class RootConfig {
}
package com.rollin;
import java.io.File;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
/**
*
* @author rollin
*
*/
public class AppTomcat {
// 端口号
private static int port = 9090;
public static void main(String[] args) {
start();
}
private static void start() {
try {
// TODO Auto-generated method stub
// 创建Tomcat服务器
Tomcat tomcat = new Tomcat();
// 设置端口号
tomcat.setPort(port);
tomcat.getHost().setAutoDeploy(false);
// 创建Context上下文
// 读取项目路径
StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("src/main/webapp").getAbsolutePath());
// 禁止重新载入
ctx.setReloadable(false);
// class文件读取地址
File additionWebInfClasses = new File("target/classes");
// 创建WebRoot
WebResourceRoot resources = new StandardRoot(ctx);
// tomcat内部读取Class执行
resources.addPreResources(
new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
tomcat.start();
System.out.println("tomcat启动成功...");
// 服务阻塞等待
tomcat.getServer().await();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("tomcat启动失败...");
}
}
}
⑦、项目添加逻辑层
Service层:
package com.rollin.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public String index() {
return "UserService 访问成功!!--RollinSpark";
}
}
Controller层:
package com.rollin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rollin.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/userIndex",produces = "text/html;charset=UTF-8")
public String index() {
return userService.index();
}
}
本文到这里基本结束,集成JSP就省略了,大致就是配置视图解析器,然后跳转页面.....有需要DEMO的可以加我扣扣,2485290255,记得备注说明!!!