java-springboot-tomcat 搭建

一、优点
1.搭建一个 spring mvc 项目只需要1分钟
2.不用配置 web.xml 和 各种xml
3.多种方式启动项目
3.1 开发阶段使用 mvn 命令运行
3.2 打成 war 包后,使用 java -jar 命令运行
3.3 打成 war 包后,部署到 tomcat 运行


二、资料
官网:http://projects.spring.io/spring-boot/


三、搭建第一个 springboot 项目
1.环境
ide:idea
jdk:1.7
springboot:1.5.6


2.创建 idea module,命名为 SbootDemo2
2.1 file=>new=>mudule...
2.2 选择 Spring Initializr:module sdk 选择 1.7,其他默认。下一步
2.3 填写一些包信息,packaging 选择 war,java version 选择 1.7;下一步
2.4 找到 web 节点,勾选 web 复选框,生成一个 web 项目;(这步很关键,可以生成一个 ServletInitializer 类);下一步
2.5 选择生成路径,点击 finish,完成一个 springboot 项目创建;


3.项目也可使用 spring 提供的在线生成工具,在线生成
地址:https://start.spring.io/


4.打开项目将看到有两个类

SbootDemo2Application.java //springboot 主项目,应用程序入口
ServletInitializer.java //tomcat 启动入口



4.1 SbootDemo2Application.java ,mvn spring-boot:run 命令、java -jar 命令启动支持

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SbootDemo2Application {


	public static void main(String[] args) {
		System.out.println("...... 准备启动");
		SpringApplication.run(SbootDemo2Application.class, args);
		System.out.println("...... 已启动,访问 http://local:8080/");
	}
}



4.2 ServletInitializer.java  tomcat 启动支持
package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.web.context.WebApplicationContext;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;


/**
 * tomcat 启动 spring boot
 */
public class ServletInitializer extends SpringBootServletInitializer {


	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		System.out.println("...... configure");
		return application.sources(SbootDemo2Application.class);
	}


	@Override
	protected WebApplicationContext run(SpringApplication application) {
		System.out.println("...... run");
		return super.run(application);
	}


	public ServletInitializer() {
		super();
		System.out.println("...... ServletInitializer");
	}


	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		System.out.println("...... onStartup");
		super.onStartup(servletContext);
	}


	@Override
	protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
		System.out.println("...... createRootApplicationContext");
		return super.createRootApplicationContext(servletContext);
	}


	@Override
	protected SpringApplicationBuilder createSpringApplicationBuilder() {
		System.out.println("...... createSpringApplicationBuilder");
		return super.createSpringApplicationBuilder();
	}
}



5.新建一个 mvc 的 controller 



package com.example.demo;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Date;


@RestController
public class HeloController {
    @RequestMapping("/")
    public String hello(){
        System.err.println("HeloController.hello();");
        return "hello demo2 ..."+ new Date();
    }
}





6.运行项目
6.1 mvn 命令运行

1>使用 cd 命令定位到项目根目录
2>输入 mvn spring-boot:run 命令
3>看到以下内容表示运行成功了
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)


4>打开浏览器输入:http://local:8080/ 查看运行结果
5>springboot 默认使用 8080 端口,改变端口需要配置 application.properties 文件


6.2 使用 java -jar 命令,运行项目
1> 使用 cd 命令定位到项目根目录
2> 将项目打成 war 包; 命令:mvn clean package -Dmaven.test.skip=true
3> 复制已经打完的 war 包完整路径,执行以下命令运行
java -jar /sboot-demo2/target/sboot-demo2-0.0.1-SNAPSHOT.war
4>看到以下内容表示运行成功了
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)


5>打开浏览器输入:http://local:8080/ 查看运行结果
6>springboot 默认使用 8080 端口,改变端口需要配置 application.properties 文件


6.3 部署到 tomcat 运行
1>将打成的 war 包,复制到 tomcat webapps 目录,运行 bin 目录中的 startup.sh 


6.4 配置开发环境中的 tomcat ,也可以使用 开发工具 运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值