spring-boot-hello-world

本文介绍Spring Boot的基本概念及其简化Spring应用配置的方式,并演示了如何搭建一个简单的Spring Boot应用,包括项目结构、依赖管理、控制器编写及视图展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是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包运行

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

mav命令运行

mvn spring-boot-demo:run

测试

在浏览器中输入如下地址便可访问服务。
http://127.0.0.1:8080/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值