随笔 - 8  文章 - 0  评论 - 2 

Spring Boot 教程(3)Spring Boot 基础配置

Spring Boot 基础配置

1.自定义Banner配置

在resource目录下新建banner.txt,在该txt文件中写需要展示的内容即可替换spring boot 的默认banner。

如何生成类似官方的banner呢?

登录http://patorjk.com/software/taag/网站,设计banner。

如何关闭banner?

修改启动类的相关配置

@SpringBootApplication
public class SbbaseconfigApplication {

    public static void main(String[] args) {
       // SpringApplication.run(SbbaseconfigApplication.class, args);
        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(SbbaseconfigApplication.class);
        SpringApplication build = springApplicationBuilder.build();
        build.setBannerMode(Banner.Mode.OFF); //关闭banner
        build.run(args);
    }

}

2.内嵌容器

Spring Boot 提供了三种内嵌容器:tomcat、jetty、Undertow。必须引入spring-boot-starter-web。

spring-boot-starter-web 默认使用的是tomcat。

tomcat相关配置

#修改服务器端口
server.port=8081
#修改上下文路径
server.servlet.context-path=/sbbaseconfig
#修改 tomcat Url编码
server.tomcat.uri-encoding=utf-8

怎么使用jetty或Undertow容器

首先要排除掉tomcat,再引入jetty 或Undertow

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
       <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

3.Spring 属性注入

3.1在 application.properties配置属性

book.id=1
book.name=红楼梦
book.author=曹雪芹

Book.java

/**
 * @author 飞鱼xuy
 * @gitee https://gitee.com/xuyou1028/
 */
@Component
public class Book {
    @Value("${book.id}") //  ctrl+d 复制当前行到下一行 alt+shift+下箭头移动当前行
    private Integer id;
    @Value("${book.name}")
    private  String name;
    @Value("${book.author}")
    private  String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

编写测试

@SpringBootTest
class SbbaseconfigApplicationTests {

    @Autowired
    Book book;
    @Test
    void contextLoads() {
        System.out.println(book);
    }

}

运行测试 控制台输出 Book{id=1, name='红楼梦', author='曹雪芹'} .如果遇到中文乱码的情况,需要对idea进行设置如下。

3.2 自定义properties ,然后注入

新建 book.properties

book.id=1
book.name=红楼梦
book.author=曹雪芹

在需要注入的类上使用注解@PropertySource("classpath:book.properties")。注意此注解属于spring注解,不是spring boot特有的。此注解只能注入properties文件中的注解,不能注入yaml文件里的注解。

Book.java

/**
 * @author 飞鱼xuy
 * @gitee https://gitee.com/xuyou1028/
 */
@Component
@PropertySource("classpath:book.properties")
public class Book {
    @Value("${book.id}") //  ctrl+d 复制当前行到下一行 alt+shift+下箭头移动当前行
    private Integer id;
    @Value("${book.name}")
    private  String name;
    @Value("${book.author}")
    private  String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

4.类型安全的属性注入 @ConfigurationProperties

该注解是spring boot 独有注解。使用此注解后,可以去除@value注解,降低对代码的污染。

Book2.java

@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix="book")
public class Book2 {
    private Integer id;
    private  String name;
    private  String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

测试类

@SpringBootTest
class SbbaseconfigApplicationTests {

 
    @Autowired
    Book2 book2;
    @Test
    void contextLoads2() {
        System.out.println(book2);
    }

}

5.Yaml配置 基础

*.yaml/ *.yml与properties区别

1.yml配置是有序的,properties配置是无序的。

2.yml不支持 使用PropertySource直接注入

3.支持配置列表 数据结构

注入 List(1)

application.yml

redis:
  port: 6379
  hosts:
    - 192.168.20.1
    - 192.168.20.2
    - 192.168.20.3
    - 192.168.20.4

注入类

@Component
@ConfigurationProperties(prefix ="redis" )
public class RedisCluster {
    private Integer port;
    private List<String> hosts;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public List<String> getHosts() {
        return hosts;
    }

    public void setHosts(List<String> hosts) {
        this.hosts = hosts;
    }

    @Override
    public String toString() {
        return "RedisCluster{" +
                "port=" + port +
                ", hosts=" + hosts +
                '}';
    }
}

测试类

@SpringBootTest
class SbbaseconfigApplicationTests {

    @Autowired
    RedisCluster redisCluster;
    
    @Test
    void contextLoads4() {
        System.out.println(redisCluster);
    }

}

控制台输出结果:RedisCluster{port=6379, host=[192.168.20.1, 192.168.20.2, 192.168.20.3, 192.168.20.4]}

注入 List(2)

application.yml

redis:
  port: 6379
  hosts:
    - 192.168.20.1
    - 192.168.20.2
    - 192.168.20.3
    - 192.168.20.4
  redisList:
    - port: 6379
      host: 192.168.30.1
    - port: 6378
      host: 192.168.30.2

RedisCluster.java

@Component
@ConfigurationProperties(prefix ="redis" )
public class RedisCluster {
    private Integer port;
    private List<String> hosts;
    List<Redis> redisList;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public List<String> getHosts() {
        return hosts;
    }

    public void setHosts(List<String> hosts) {
        this.hosts = hosts;
    }

    public List<Redis> getRedisList() {
        return redisList;
    }

    public void setRedisList(List<Redis> redisList) {
        this.redisList = redisList;
    }

    @Override
    public String toString() {
        return "RedisCluster{" +
                "port=" + port +
                ", hosts=" + hosts +
                ", redisList=" + redisList +
                '}';
    }
}

控制台打印;RedisCluster{port=6379, hosts=[192.168.20.1, 192.168.20.2, 192.168.20.3, 192.168.20.4], redisList=[Redis{port=6379, host='192.168.30.1'}, Redis{port=6378, host='192.168.30.2'}]}

6.Profile

在实际开发之中 由于开发(dev) 测试(test) 运行(product)的环境不同,有可能需要选择不同的配置文件。profile就是用来解决这个问题的。

新建application-dev.properties、application-test.properties或者application-dev.yaml、application-test.yaml

新建application-dev.properties

#端口配置
server.port=7777

新建新建application-test.properties

#端口配置
server.port=8888

激活application-dev.properties这个配置

在appliation.properties中配置

spring.profiles.active=dev

重新启动服务

此时服务端口为7777.如果配置spring.profiles.active=test,服务端口为8888。这样就做到了在不同环境使用不同的配置。

欢迎提出您的宝贵建议。如果对您有帮助,请点推荐支持一下。

源码地址:https://gitee.com/xuyou1028/spring-boot-tutorial/tree/master/sbbaseconfig

posted on 2020-08-31 13:49  飞鱼xuy  阅读(40)  评论(0编辑  收藏