手写SpringBoot框架

1.Maven依赖

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.weichai</groupId>
	<artifactId>SpringBoot_Write</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot_Write Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>

		<!--Java语言操作tomcat -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-core</artifactId>
			<version>9.0.10</version>
		</dependency>

		<!-- tomcat对jsp支持 -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jasper</artifactId>
			<version>9.0.10</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.1.0.RELEASE</version>
			<scope>compile</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.1.0.RELEASE</version>
			<scope>compile</scope>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>SpringBoot_Write</finalName>
	</build>
</project>

2.创建tomcat容器启动类

package com.weichai.springboot;

import java.io.File;

import javax.servlet.ServletException;

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.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

/**
    *    手写SpringBoot App启动类
 * @author linhaiy
 * @date 2019.02.13
 */
public class App {

	public final static int PORT = 8090;
	
	public static void main(String[] args) throws ServletException, LifecycleException {
		// TODO Auto-generated method stub
		start();
	}

	public static void start() throws ServletException, LifecycleException {
		// 创建Tomcat容器
		Tomcat tomcatServer = new Tomcat();
		// 端口号设置
		tomcatServer.setPort(PORT);
		// 读取项目路径 加载静态资源
		StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").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(), "/"));
		tomcatServer.start();
		// 异步等待请求执行
		tomcatServer.getServer().await();
	}
}

3.根配置类

package com.weichai.springboot.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
    *   根配置
 * @author linhaiy
 * @date 2019.02.13
 */
@Configuration
@ComponentScan("com.weichai.springboot")
public class RootConfig {

}

4.SpringMVC配置信息加载类

package com.weichai.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * SpringMVC配置信息
 * @author linhaiy
 * @date 2019.02.13
 */

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.weichai.springboot.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {

	/**
	 * springboot 整合jsp 最好是war
	 * InternalResourceViewResolver
	    *     创建SpringMVC视图解析器
	 * @return
	 */
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		// 可以在JSP页面中通过 ${} EL表达式访问beans
		viewResolver.setExposeContextBeansAsAttributes(true);
		return viewResolver;
	}
}

5.SpringMVC DispatcherServlet加载类

package com.weichai.springboot.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
    *    加载SpringMVC DispatcherServlet
 * @author linhaiy
 *
 */
public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	/**
	    *   加载根配置信息 spring核心
	 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[] {RootConfig.class};
	}

	/**
	 * springmvc 加载 配置信息
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		// TODO Auto-generated method stub
		return new Class [] {WebConfig.class};
	}

	/**
	 * SpringMVC 拦截url映射 拦截所有请求
	 */
	@Override
	protected String[] getServletMappings() {
		// TODO Auto-generated method stub
		return new String[] {"/"};
	}

}

6.业务逻辑层

package com.weichai.springboot.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

	public String index() {
		return "springboot 2.0  我正在加载UserService";
	}
}

7.控制层测试

package com.weichai.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
    *     跳转页面
 * @author linhaiy
 * @date 2019.02.13
 */
@Controller
public class UserController {

	@RequestMapping("/pageIndex")
	public String pageIndex() {
		return "pageIndex";
	}
}
package com.weichai.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.weichai.springboot.service.UserService;

@RestController
public class IndexController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/index", produces = "text/html;charset=UTF-8")
	public String index() {
		return userService.index();
	}
}

8.测试映射页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>手写SpringBoot</title>
</head>
<body>

	<h1>纯手写SpringBoot框架,感谢支持海哥!</h1>

</body>
</html>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值