聚富彩票平台出租修复

聚富彩票平台出租修复

1148916888.com

聚富彩票平台出租修复

聚富彩票平台出租修复

配置文件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

扫描二维码关注公众号,回复: 1776811 查看本文章

##设置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

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 {br/>@Value("${test.springboot.name}")
private String name;
br/>@Value("${test.springboot.context}")
private String context;
br/>@Value("${test.springboot.desc}")
private String desc;
br/>//测试随机数配置
@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;
}   

}

Application.java

package com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

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.SpringJUnit4Cla***unner;

import com.lyd.entity.TestProperties;

@RunWith(SpringJUnit4Cla***unner.class)br/>@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());
}

}

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>

项目目录
这里写图片描述

运行getHello方法,可以读到application.properties中的数据:

猜你喜欢

转载自blog.51cto.com/13842278/2133794