一起学JAVA之《spring boot》05

来源:互联网 发布:网络诈骗手段方式 编辑:程序博客网 时间:2024/05/21 23:15

一、导航

本节内容简介:
1. 新建一个测试自定义配置的模块
2. 自定义配置
3. 自定义配置文件
4. 配置文件属性验证

二、新建一个测试自定义配置的模块

idea创建子模块流程
- 在项目上新建模块
新建子模块

  • 然后选择create from archetype ,下面选择快速开始模块(也可以是其他模块)

    新建子模块quickstart

  • 接着就是下一步直到完成,中间会配置项目名字等信息,就按照标准的配置就是了

有时候我们创建一个模块之后,发现下载模块信息很慢,就可以配置阿里云的私服
阿里私服镜像地址

<mirror>      <id>alimaven</id>      <name>aliyun maven</name>      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>      <mirrorOf>central</mirrorOf>        </mirror>

三、自定义配置

自定义配置是读取我们自己的配置文件或在application中的自定义数据

  1. 在创建的模块中,创建一个ConfigController
/** * 配置测试类 * The type Config controller. */@RestControllerpublic class ConfigController {    @Value("${my.name : default_Like}")    private  String name;    @Value("${my.age}")    private  int age;    @RequestMapping("/testConfigure")    public String testConfigure(){        return  "name = "+name +" age =  "+age;    }}
  1. 在配置文件中,加如下配置
server.port=8083#自定义属性my.name=my.age=20

启动运行,访问:http://localhost:8083/testConfigure
结果:name = default_Like age = 20

如果my.name 属性设置了值,就会返回的是my.name的值,否则就是默认的值

四、自定义配置文件

有时候我们在application中定义属性过多,这个时候我们就可以自定义配置文件,如下

  • 自定义配置文件person.properties,位置在src/main/resources中
person.name=likeperson.age=22person.gender=man
  • 自定义配置文件需要引入一个自动配置的jar
 <!--自定义配置所需jar包-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-configuration-processor</artifactId>            <optional>true</optional>        </dependency>
  • 创建PersonConfig类
package com.likeoak.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * 自定义配置文件 * The type Person config. */@Component@ConfigurationProperties(prefix = "person")@PropertySource(value = "classpath:person.properties")public class PersonConfig {    private String name;    private Integer age;    private String gender;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    @Override    public String toString() {        return "PersonConfig{" +                "name='" + name + '\'' +                ", age=" + age +                ", gender='" + gender + '\'' +                '}';    }}

代码解释
@Component 将这个配置类声明为一个bean
@ConfigurationProperties 自定义配置注解,prefix 指的是当前配置文件的前缀
@PropertySource 扫描配置文件路径注解,在1.5以前可以用@ConfigurationProperties的location注解来指定文件路径,1.5之后就用这个注解来指定

  • 配置测试类
/** * 配置测试类 * The type Config controller. */@RestControllerpublic class ConfigController {    @Value("${my.name : default_Like}")    private  String name;    @Value("${my.age}")    private  int age;    @Autowired    private PersonConfig personConfig;    @RequestMapping("/testConfigure")    public String testConfigure(){        return  "name = "+name +" age =  "+age;    }    @RequestMapping("/testPersonConfig")    public String  testPersonConfig(){        return personConfig.toString();    }}

启动,访问http://localhost:8083/testPersonConfig
结果:PersonConfig{name=’like’, age=22, gender=’man’}

五、配置文件属性验证

spring boot 也可以校验外部配置,默认使用JSR-303,在@ConfigurationProperties类添加JSR-303 javax.validation约束注解

1. 测试没有加验证的情况

  • 删除person.properties配置文件中的person.name属性

启动程序,访问http://localhost:8083/testPersonConfig
结果:PersonConfig{name=’null’, age=22, gender=’man’}

  1. 在代码上加入验证
/** * 自定义配置文件 * The type Person config. */@Component@ConfigurationProperties(prefix = "person")@PropertySource(value = "classpath:person.properties")public class PersonConfig {    @NotNull    private String name;    private Integer age;    private String gender;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    @Override    public String toString() {        return "PersonConfig{" +                "name='" + name + '\'' +                ", age=" + age +                ", gender='" + gender + '\'' +                '}';    }

启动:
结果:

***************************APPLICATION FAILED TO START***************************Description:Binding to target PersonConfig{name='null', age=22, gender='man'} failed:    Property: person.name    Value: null    Reason: 不能为nullAction:
原创粉丝点击