spring boot 简单搭建

一、利用maven,引入相关依赖(pom.xml)

<!-- 继承spring Boot官方的配置,能够继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小化-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
</properties>

<!--至关于通知打包插件,这是一个web项目-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

<!--spring-boot-maven-plugin提供了直接运行项目的插件,咱们能够直接mvn spring-boot:run运行-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

二、新建controller层(HelloWorldController.java)

考虑到实际项目中,均将请求映射至对应的html或jsp页面,故此处将其 return 跳转至相应的页面上。html

@SpringBootApplication
@Controller
public class HelloWorldController {

    //映射至对应的html
    @RequestMapping("index")
    public String index() {
        return "index";
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldController.class, args);
    }
}

@RequestMapping注解代表该方法处理那些URL对应的HTTP请求java

@SpringBootApplication能够替代 @Configuration,@EnableAutoConfiguration,@ComponentScan三个注解。(备注:使用Spring注解继承实现)。web

这里的index方法,跳转的便是对应的\src\main\resources\templates路径下的index.html。spring

注意:此处文件夹必须是templates,“约定优于配置”,不然项目启动失败浏览器

index.html 以下:微信

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
</head>
<body>
    这是index页面
</body>
</html>

三、配置属性文件

Spring boot 的默认配置文件是 resources 下的 application.properties,不可重命名,spring boot约定俗成app

使用配置文件以后,spring boo启动时,会自动把配置信息读取到spring容器中,并覆盖spring boot的默认配置,jsp

//启动端口
server.port=8081

四、项目启动

在拥有 @SpringBootApplication 注解的类中,使用 SpringApplication 的 run 方法能够经过JAR启动项目maven

在HelloWorldController运行main函数,项目启动。浏览器中输入:http://localhost:8081/index 。页面以下:函数

项目结构图

项目总体结构以下:

项目结构图

参考连接:

相关文章
相关标签/搜索