美文网首页
Spring Boot2使用小记

Spring Boot2使用小记

作者: 九号自行车司机 | 来源:发表于2020-05-15 17:56 被阅读0次

FAQ

如何快速搭建一个spring boot项目?

使用Spring Initializr可以快速获取一个spring boot项目。下载解压生成的项目文件,可以看到与普通maven项目相比
使用spring boot搭建的jar项目有以下不同

  1. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mygroup</groupId>
    <artifactId>myapplication</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myprojectname</name>
    <description>name for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>
  1. src\main\java目录下带main函数的java类
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);
    }

}
  1. src\main\resources目录下需要一个名称为application.properties的配置文件

  2. src\test\java目录下的测试类

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApplicationTests {

    @Test
    void contextLoads() {
    }

}

使用spring boot搭建的war项目有以下不同

  1. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mygroup</groupId>
    <artifactId>myartifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>myprojectname</name>
    <description>name for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>
  1. src\main\java目录下带main函数的java类
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);
    }

}
  1. src\main\java目录下继承SpringBootServletInitializer的java类
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}
  1. src\main\resources目录下需要一个名称为application.properties的配置文件,同时还有两个文件夹statictemplates

  2. src\test\java目录下的测试类

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApplicationTests {

    @Test
    void contextLoads() {
    }

}

如何使用log4j2作为默认日志工具?

  1. spring-boot-starter依赖下排除spring-boot-starter-logging依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
        <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
</dependency>
  1. 增加spring-boot-starter-log4j2依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
  1. src\main\resources目录下增加log4j2-spring.xml作为log4j2的配置文件

如何覆盖spring-boot-dependencies推荐的依赖版本?

pom.xml中的properties标签中明确指定自己想要的版本

    <properties>
        <mysql.version>5.1.27</mysql.version>
    </properties>

如何执行使用spring boot插件打包而成的jar包?

查看相关文档可以知道:使用spring boot的项目打包时,会生成一种特殊格式的jar文件,这使得执行jar文件变得相当简单,只需命令行执行:

java -jar target/myapplication-0.0.1-SNAPSHOT.jar

还可以通过在pom文件中修改配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

这样打包而成的jar文件就成为了一个“完全可执行的jar包”,可以直接在命令行这么执行:

./target/myapplication-0.0.1-SNAPSHOT.jar

读取配置文件时中文乱码?

放弃.properties格式,使用.yaml格式作为配置文件的文件格式。.properties格式默认会使用ISO 8859-1作为字符编码,而.yaml格式默认支持UTF-8编码。

接口返回的JSON数据中日期不是时间戳?

配置文件中加入以下配置:

spring.jackson.serialization.write-dates-as-timestamps=true

参考资料

相关文章

网友评论

      本文标题:Spring Boot2使用小记

      本文链接:https://www.haomeiwen.com/subject/lgiboctx.html