博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-boot-hello-world
阅读量:2430 次
发布时间:2019-05-10

本文共 3068 字,大约阅读时间需要 10 分钟。

什么是spring-boot

原来JavaEE工程使用spring的时候需要引入很多相关的依赖包,对于不怎么熟悉spring相关依赖的同学还是有一定难度的。spring-boot就是为了简化spring的手动配置依赖,spring-boot本身集成了tomcat,不需要再打成war发布到tomcat中,直接运行即可。

工程结构

这里写图片描述

依赖包

配置pom.xml

org.springframework.boot
spring-boot-starter-parent
1.4.2.RELEASE
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-thymeleaf

以jar包形式引入jQuery

org.webjars.bower
jquery
3.1.1
org.springframework.boot
spring-boot-maven-plugin

工程代码

application.properties

#服务端口通过该配置修改server.port=8080########################################################  ###THYMELEAF (ThymeleafAutoConfiguration)  ########################################################  #spring.thymeleaf.prefix=classpath:/templates/  #spring.thymeleaf.suffix=.html  #spring.thymeleaf.mode=HTML5  #spring.thymeleaf.encoding=UTF-8  # ;charset=
is added #spring.thymeleaf.content-type=text/html # set to false for hot refresh spring.thymeleaf.cache=false

Application.java

主程序,工程启动入口。

package com.itclj;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}

TemplateController.java

package com.itclj.demo.controller;import java.util.Map;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.ComponentScan;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@ComponentScanpublic class TemplateController {    private final Logger logger = LoggerFactory.getLogger(this.getClass());      @RequestMapping("/")    String home() {        return "index";    }    @RequestMapping("/hello")      public String helloHtml(Map
map){ logger.debug("==========debug"); logger.warn("===========warn"); logger.info("===========info"); logger.error("==========error"); map.put("hello","from TemplateController.helloHtml"); return"/hello"; } }

index.html

    首页    
首页
=================

logback.xml

spring-boot-demo.log
spring-boot-demo.%i.log.zip
1
3
10MB
%date [%thread] %-5level %logger - %msg%n

运行

IDE中运行

在IDE中直接运行Application.java中的main函数即可启动应用。

发布成jar包运行

  1. maven打成jar包,或IDE打成jar包
  2. 执行java -jar spring-boot-demo.jar

mav命令运行

mvn spring-boot-demo:run

测试

在浏览器中输入如下地址便可访问服务。

你可能感兴趣的文章
About Recommender Systems
查看>>
jason数据格式
查看>>
金山快盘的安全性太差了
查看>>
KDD Cup2011
查看>>
“相关性”时代的到来
查看>>
腾讯盛大百度版咆哮体
查看>>
opencv阈值法分割图像
查看>>
OpenCV资料
查看>>
常见域名后缀词典
查看>>
python编辑器对比和推荐
查看>>
极阅和微精
查看>>
回顾我的2011
查看>>
Outbrain
查看>>
视频站点下载地址汇总
查看>>
智能Web算法第二版前言和译者序
查看>>
第一本docker书学习笔记1-3章
查看>>
RPC实践(二)JsonRPC实践
查看>>
RPC实践(三)Hessian实践
查看>>
Zookeeper实践(四)zookeeper的WEB客户端zkui使用
查看>>
RPC实践(四)Dubbo实践
查看>>