本文共 3068 字,大约阅读时间需要 10 分钟。
原来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
#服务端口通过该配置修改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
主程序,工程启动入口。
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); }}
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(Mapmap){ logger.debug("==========debug"); logger.warn("===========warn"); logger.info("===========info"); logger.error("==========error"); map.put("hello","from TemplateController.helloHtml"); return"/hello"; } }
首页 首页=================
spring-boot-demo.log spring-boot-demo.%i.log.zip 1 3 10MB %date [%thread] %-5level %logger - %msg%n
在IDE中直接运行Application.java中的main函数即可启动应用。
mvn spring-boot-demo:run
在浏览器中输入如下地址便可访问服务。