构建第一个 SpringBoot 工程

SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程.

设计的目标

  • 为所有使用 Spring 的开发者提供一个更简单,快速的入门体验
  • 提供一些常见的功能、如监控、WEB容器,健康,安全等功能
  • 干掉XML,遵循规范,开箱即用

项目搭建

创建项目的时候使用Spring Initializr,创建的项目结构如下

- src
    -main
        -java
            -package
                #启动类
                -SpringbootApplication    
        -resouces
            #存放静态资源如 js/css/images 等
            - statics
            #存放 html 模板文件
            - templates
            #主要的配置文件,SpringBoot启动时候会自动加载application.yml/application.properties        
            - application.yml
    #测试文件存放目录        
    -test
 # pom.xml 文件是Maven构建的基础,里面包含了我们所依赖JAR和Plugin的信息
- pom

pom.xml 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <name>spring-boot-web</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.winner</groupId>
    <artifactId>spring-boot-web</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--默认内嵌Tomcat容器,可以根据需要进行替换-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--用来进行热部署的插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

主函数入口

一个项目中切记不要出现多个main函数,否在在打包的时候spring-boot-maven-plugin将找不到主函数!

package com.winner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * 主函数启动类
 * @author winner_0715
 * @date 2018/11/28
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.out.println(" springApplication run !");
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        // 目的是
        return args -> {
            System.out.println("SpringBoot默认为我们提供的Bean:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }
}

编写我们的Controller

package com.winner.web;

import com.winner.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author winner_0715
 * @description  HelloController
 * @date 2018/11/28
 */
@RestController
public class HelloController {

    @RequestMapping("/hello/string")
    public String helloString(){
        return "hello world";
    }

    /**
     * 因为使用了@RestController
     * 所以不需要加@ResponseBody注解
     * @RestController=@Controller+@ResponseBody
     * @return
     */
    @RequestMapping("/hello/model")
    public User helloModel(){
        return new User.Builder()
                .userName("name")
                .email("email")
                .build();
    }
}

认识Spring Boot主配置文件

application.properties

从启动日志中可以发现,SpringBoot默认的端口是8080,如果端口被占用我们可以通过修改配置文件来解决!

Tomcat started on port(s): 8080 (http) with context path ''

修改默认的配置(还带提示的,很给力,配置了热部署的话修改配置文件就会重启生效)

# 修改默认的端口8080
server.port=8888
# 定义上下文路径
server.servlet.context-path=/spring-boot-web

看下此时控制台的输出

Tomcat started on port(s): 8888 (http) with context path '/spring-boot-web'

这样的话访问就需要带上项目名了!

猜你喜欢

转载自www.cnblogs.com/winner-0715/p/10058363.html