配置文件详解:自定义属性、随机数、多环境配置等
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter2-1-1</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter2-1-1</name>
|
||||
<description></description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.didispace.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/5 下午12:16.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Component
|
||||
public class BlogProperties {
|
||||
|
||||
@Value("${com.didispace.blog.name}")
|
||||
private String name;
|
||||
@Value("${com.didispace.blog.title}")
|
||||
private String title;
|
||||
@Value("${com.didispace.blog.desc}")
|
||||
private String desc;
|
||||
|
||||
@Value("${com.didispace.blog.value}")
|
||||
private String value;
|
||||
@Value("${com.didispace.blog.number}")
|
||||
private Integer number;
|
||||
@Value("${com.didispace.blog.bignumber}")
|
||||
private Long bignumber;
|
||||
@Value("${com.didispace.blog.test1}")
|
||||
private Integer test1;
|
||||
@Value("${com.didispace.blog.test2}")
|
||||
private Integer test2;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Integer getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(Integer number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Long getBignumber() {
|
||||
return bignumber;
|
||||
}
|
||||
|
||||
public void setBignumber(Long bignumber) {
|
||||
this.bignumber = bignumber;
|
||||
}
|
||||
|
||||
public Integer getTest1() {
|
||||
return test1;
|
||||
}
|
||||
|
||||
public void setTest1(Integer test1) {
|
||||
this.test1 = test1;
|
||||
}
|
||||
|
||||
public Integer getTest2() {
|
||||
return test2;
|
||||
}
|
||||
|
||||
public void setTest2(Integer test2) {
|
||||
this.test2 = test2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.didispace.web;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping("/hello")
|
||||
public String index() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# 服务端口
|
||||
server.port=1111
|
||||
@@ -0,0 +1,2 @@
|
||||
# 服务端口
|
||||
server.port=3333
|
||||
@@ -0,0 +1,2 @@
|
||||
# 服务端口
|
||||
server.port=2222
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
com.didispace.blog.name=程序猿DD
|
||||
com.didispace.blog.title=Spring Boot教程
|
||||
com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》
|
||||
|
||||
# 随机字符串
|
||||
com.didispace.blog.value=${random.value}
|
||||
# 随机int
|
||||
com.didispace.blog.number=${random.int}
|
||||
# 随机long
|
||||
com.didispace.blog.bignumber=${random.long}
|
||||
# 10以内的随机数
|
||||
com.didispace.blog.test1=${random.int(10)}
|
||||
# 10-20的随机数
|
||||
com.didispace.blog.test2=${random.int[10,20]}
|
||||
|
||||
# 多环境配置文件激活属性
|
||||
spring.profiles.active=dev
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.service.BlogProperties;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @blog http://blog.didispace.com
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ApplicationTests.class);
|
||||
|
||||
@Autowired
|
||||
private BlogProperties blogProperties;
|
||||
|
||||
|
||||
@Test
|
||||
public void getHello() throws Exception {
|
||||
Assert.assertEquals(blogProperties.getName(), "程序猿DD");
|
||||
Assert.assertEquals(blogProperties.getTitle(), "Spring Boot教程");
|
||||
Assert.assertEquals(blogProperties.getDesc(), "程序猿DD正在努力写《Spring Boot教程》");
|
||||
|
||||
log.info("随机数测试输出:");
|
||||
log.info("随机字符串 : " + blogProperties.getValue());
|
||||
log.info("随机int : " + blogProperties.getNumber());
|
||||
log.info("随机long : " + blogProperties.getBignumber());
|
||||
log.info("随机10以下 : " + blogProperties.getTest1());
|
||||
log.info("随机10-20 : " + blogProperties.getTest2());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user