SpringBoot之入门配置文件-配置文件
介绍:example02
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties中完成一些属性配置就能开启各模块的应用。
配置文件application.properties的使用,主要用来配置数据库连接、日志相关配置等。除了这些配置内容之外,本文将具体介绍一些在application.properties配置中的其他特性和使用方法。
我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义:
application.properties
test.springboot.name=学习
test.springboot.context=我
#参数间引用
test.springboot.desc=${test.springboot.context}爱${test.springboot.name}
#随机数
##随机字符串
test.springboot.value=${random.value}
##随机int
test.springboot.number=${random.int}
##随机long
test.springboot.bignumber=${random.long}
##10以内的随机数
test.springboot.test1=${random.int(10)}
##10-20的随机数
test.springboot.test2=${random.int[10,20]}
#多环境配置文件激活属性
spring.profiles.active=dev
# 服务端口
server.port=9090
##设置utf-8编码集
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
test.springboot.desc参数引用了上文中定义的name和context属性,最后该属性的值就是:我爱学习
引用值:通过@Value(“${属性名}”)注解来加载对应的配置属性,具体如下:
创建实体类:TestProperties
package com.lyd.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class TestProperties {
@Value("${test.springboot.name}")
private String name;
@Value("${test.springboot.context}")
private String context;
@Value("${test.springboot.desc}")
private String desc;
//测试随机数配置
@Value("${test.springboot.value}")
private String value;
//get/set方法...
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
Application.java
package com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author : xiaolin
* @Description: Created by Administrator on 2017/11/22.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
ApplicationTest.java单元测试类
package com.lyd;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetConstraintValidatorList;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lyd.entity.TestProperties;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTest {
private static final Log log = LogFactory.getLog(ApplicationTest.class);
@Autowired
private TestProperties testProperties;
@Test
public void getHello(){
Assert.assertEquals(testProperties.getName(), "学习");
Assert.assertEquals(testProperties.getDesc(), "我爱学习");
System.out.println(testProperties.getName());
log.info("随机字符串测试:"+testProperties.getValue());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
pom文件
<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>
<parent>
<groupId>com.lyd</groupId>
<artifactId>springboot-lyd</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>example02</artifactId>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
项目目录
运行getHello方法,可以读到application.properties中的数据:
相关内容
-
什么是boot.ini文件错误ini的非法解决方案
什么是boot.ini文件错误ini的非法解决方案,,千奇百怪的电脑问...
-
联想笔记本电脑主板颜色|联想笔记本颜色配置文
联想笔记本电脑主板颜色|联想笔记本颜色配置文件,,联想笔记本...
-
2700元的高性价比全新奔腾g645平台的入门配置推
2700元的高性价比全新奔腾g645平台的入门配置推荐_intel计算...
-
boot怎么恢复默认设置|boot重新设置方法
boot怎么恢复默认设置|boot重新设置方法,,1. boot重新设置方法...
-
电脑教程word入门|电脑word入门介绍教程
电脑教程word入门|电脑word入门介绍教程,,电脑教程word入门wor...
-
spring中基于BeanPostProcessor动态替换指定的b
spring中基于BeanPostProcessor动态替换指定的bean,文件,定义...
-
3000元SNB入门办公配置推荐
3000元SNB入门办公配置推荐,,依靠优良的性能和低廉的价格被新S...
-
从零入门学习电脑的电脑小爱需要了解的知识分享
从零入门学习电脑的电脑小爱需要了解的知识分享,的话,如果自己...
-
1. spring5.0源码讲解-bean的加载过程|一
1. spring5.0源码讲解-bean的加载过程|一,接口,配置文件,#spri...
-
CD快捷键|cd入门简单快捷键
CD快捷键|cd入门简单快捷键,,CD快捷键CAD中另存为的快捷键是CT...
-
windows电脑入门如何使用输入法输入汉字
windows电脑入门如何使用输入法输入汉字,,注释:汉字输入是Windo...
-
电脑出现bootmgr|电脑出现BOOTMGR开不了机怎么
电脑出现bootmgr|电脑出现BOOTMGR开不了机怎么办,,电脑出现BOO...
-
电脑入门基础教程|电脑入门零基础自学
电脑入门基础教程|电脑入门零基础自学,,电脑入门基础教程年轻...
-
01- 第一天 spring boot2.3.1 +vue3.0 后台管理
01- 第一天 spring boot2.3.1 +vue3.0 后台管理系统的研发,自...
-
电脑刷安卓boot|电脑刷安卓11
电脑刷安卓boot|电脑刷安卓11,,电脑刷安卓11国内行货版本的坚...