栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring Boot 学习笔记(4):配置properties(1)

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 学习笔记(4):配置properties(1)

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

application.properties的使用,主要用来配置数据库连接、日志相关配置等。除了这些配置内容之外,本文将具体介绍一些在application.properties配置中的其他特性和使用方法。

配置文件

SpringBoot使用一个全局的配置文件,配置文件名是固定的;

•application.properties

•application.yml

配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;

在配置文件中直接写:

name=Isea533
server.port=8080

.yml格式的配置文件如:

name: Isea533
server:
    port: 8080

注意:使用.yml时,属性名的值和冒号中间必须有空格,如name: HOWD正确,name:HOWD就是错的。

属性配置文件的位置

spring会从classpath下的/config目录或者classpath的根目录查找application.propertiesapplication.yml。/config优先于classpath根目录

自定义属性配置

在 application.properties 写入如下配置内容

    my1.age=21
    my1.name=HOWD

然后定义demoProperties.java文件,用来映射我们在application.properties中的内容,这样一来我们就可以通过操作对象的方式来获得配置文件的内容了

@Component@ConfigurationProperties(prefix = "my1")public class demoProperties {    private int age;    private String name;    // 省略 get set}

image

当你加入ConfigurationProperties注解时,便变弹出这个警告,顺着这条警告右边的open documentation会打开Spring Boot官方文档。

B.3 Generating Your Own metadata by Using the Annotation Processor
You can easily generate your own configuration metadata file from items annotated with @ConfigurationProperties by using the spring-boot-configuration-processor jar. The jar includes a Java annotation processor which is invoked as your project is compiled. To use the processor, include a dependency on spring-boot-configuration-processor.

With Maven the dependency should be declared as optional, as shown in the following example:


    org.springframework.boot
    spring-boot-configuration-processor
    true

大致意思是为了让 Spring Boot 更好的生成配置元数据文件,也只是说要添加依赖,也没说如果不这样做会造成什么影响。

接着去pom.xml去添加上面的依赖即可。

如果你是Gradle,请看官方文档Generating Your Own metadata by Using the Annotation Processor

接下来定义controller来注入demoProperties,测试我们写的代码是否正确。

@RequestMapping("/demo")@RestControllerpublic class PropertiesController {    private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);    private final demoProperties demoProperties;    @Autowired
    public PropertiesController(demoProperties demoProperties) {        this.demoProperties = demoProperties;
    }    @GetMapping("/1")    public demoProperties myProperties1() {
        log.info("=================================================================================================");
        log.info(demoProperties.toString());
        log.info("=================================================================================================");        return demoProperties;
    }

}

启动之后,在浏览器输入http://localhost:8080/demo/1

再看控制台,像我这样就完成了。

2018-09-27 15:32:32.587  INFO 3868 --- [nio-8080-exec-3] com.example.demo.PropertiesController    : =================================================================================================2018-09-27 15:32:32.587  INFO 3868 --- [nio-8080-exec-3] com.example.demo.PropertiesController    : DemoProperties{age=21, name='HOWD'}2018-09-27 15:32:32.587  INFO 3868 --- [nio-8080-exec-3] com.example.demo.PropertiesController    : =================================================================================================
多环境化配置

在真实开发中,会有多个环境,不同环境连接不同的数据库,正式总不可能连接测试的数据库。
这个时候就需要spring.profile.active,它的格式为application-{profile}.properties,这里的 application 为前缀不能改,{profile}是我们自己定义的。在resource路径下创建三个.properties后缀的文件。

  • application-dev.properties

  • application-test.properties

  • application-prod.properties

在文件中分别对应不同的server.servlet.context-path=/xxx
xxx指上面的dev、test、prod。

接着在application.properties配置文件中写入spring.profiles.active=dev,这个时候我们再次访问 http://localhost:8080/demo/1会发现没用,因为我们设置了它的context-path=/dev,所以新的路径就是 http://localhost:8080/dev/demo/1,由此可以看出来我们设置不同的配置读取的是不一样的。

随机数属性

Spring Boot 的属性配置文件中 ${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦,例如 生成 int 值、long 值或者 string 字符串。

32位随机字符串
rand.str = ${random.value}
随机int类型
rand.intid = ${random.int}
随机long类型
rand.longid = ${random.long}100以内的随机int类型
rand.number = ${random.int(100)}0-100范围内的随机int类型
rand.range = ${random.int[0,100]}



作者:HOWD
链接:https://www.jianshu.com/p/5ba39a0711aa


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号