spring-boot-hello-world
2016-12-27 10:01
411 查看
什么是spring-boot
原来JavaEE工程使用spring的时候需要引入很多相关的依赖包,对于不怎么熟悉spring相关依赖的同学还是有一定难度的。spring-boot就是为了简化spring的手动配置依赖,spring-boot本身集成了tomcat,不需要再打成war发布到tomcat中,直接运行即可。工程结构
依赖包
配置pom.xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--页面渲染模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
以jar包形式引入jQuery
<dependency> <groupId>org.webjars.bower</groupId> <artifactId>jquery</artifactId> <version>3.1.1</version> </dependency>
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
工程代码
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=<encoding> 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; @SpringBootApplication public 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 @ComponentScan public class TemplateController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/") String home() { return "index"; } @RequestMapping("/hello") public String helloHtml(Map<String,Object> map){ logger.debug("==========debug"); logger.warn("===========warn"); logger.info("===========info"); logger.error("==========error"); map.put("hello","from TemplateController.helloHtml"); return"/hello"; } }
index.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" > <head> <title>首页</title> <link rel="stylesheet" type="text/css" href="/css/index.css"/> <script src="/webjars/jquery/3.1.1/dist/jquery.min.js"></script> <script src="/js/index.js"></script> </head> <body> 首页 <div class="bkColor">=================</div> <p th:text="'Hello, iiiiiii!'" /> </body> </html>
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>spring-boot-demo.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>spring-boot-demo.%i.log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>3</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>10MB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%date [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <logger name="com.itclj.demo.controller.TemplateController" level="WARN" additivity="false"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </logger> </configuration>
运行
IDE中运行
在IDE中直接运行Application.java中的main函数即可启动应用。发布成jar包运行
maven打成jar包,或IDE打成jar包执行java -jar spring-boot-demo.jar
mav命令运行
mvn spring-boot-demo:run测试
在浏览器中输入如下地址便可访问服务。http://127.0.0.1:8080/
相关文章推荐
- SpringBoot学习之hello world
- idea springboot helloworld出错
- Springboot helloworld入门最经典例子
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- spring boot Hello World(新手入门)
- SpringBoot HelloWorld
- springboot Hello World
- idea+maven+springboot hello world
- Eclipse使用maven搭建简单spring boot application Hello World
- SpringBoot之简介及"HelloWorld"
- Spring Boot之Hello World
- Spring Boot Hello World (使用Eclipse IDE)
- spring-boot 第一天 hello-world
- 1.1 使用IntelliJ IDEA搭建Spring-Boot的"Hello World"项目
- SpringBoot之Hello World&热启动插件
- SpringBoot(二):初识SpringBoot HelloWorld
- springboot开发 第一个案例之hello,world!
- 《Springboot极简教程》 Springboot plus Kotlin :Hello,World
- SpringBoot Hello World