Java教程

SpringBoot第五章: application.properties 配置文件功能

本文主要是介绍SpringBoot第五章: application.properties 配置文件功能,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

springboot有很多配置文件,配置功能强大。

 

一. application.properties 配置tomcat服务器启动端口

如果在生产机或者测试机中遇到8080端口被占用,那么我们可以通过修改端口进行运行。

只需要修改配置文件即可:

application.properties:

# banner
spring.banner.charset=UTF-8
spring.banner.location=classpath:test.txt

# devtool
#spring.devtools.restart.enabled=true
#spring.devtools.restart.additional-paths=src/main/java

# JSP
#spring.thymeleaf.cache=false
#spring.mvc.view.prefix=/WEB-INF/views/
#spring.mvc.view.suffix=.jsp

server.port=8008

启动,测试效果:

 

二. 项目打包,把项目打成jar包。

打包:

clean package:

点击run , 就能在 target包下找到jar包。

 

三.设置 springboot启动后的访问路径

因为在spring mvc项目中,运行tomcat后,访问工程需要添加项目包名: 类似:http://localhost:8080/SpringMVC-FormProject/user/show

就有 项目包名“SpringMVC-FormProject”

在有些情况下,springboot也是需要保存 报名进行运行,或者区分 。 

那么可以在 配置文件中配置下,因为它默认是不访问项目名。

server.servlet.context-path=/SpringBoot  : 设置访问路径包名:http://localhost:8080/SpringBoot/user/show

# banner
spring.banner.charset=UTF-8
spring.banner.location=classpath:test.txt

# devtool
#spring.devtools.restart.enabled=true
#spring.devtools.restart.additional-paths=src/main/java

# JSP
#spring.thymeleaf.cache=false
#spring.mvc.view.prefix=/WEB-INF/views/
#spring.mvc.view.suffix=.jsp

server.port=8008
server.servlet.context-path=/SpringBoot

 

四. 在springboot项目中加入新的 配置文件

在项目中,会应用到很多的配置文件,比如专用于数据库连接、databases等配置文件、连接程序的自定义、秘钥配置、不同层的配置文件,当然,也可以在application.properties中进行配置

更多情况下,是会进行细分配置文件。

1. 读取配置文件数据 

随便写一个config数据 : config.properties:  少量属性

test.student.name=zhm
test.class.id=one
test.teacher.age=25

用一个controller去读:

ConfigurationPageController.java:

package com.SpringBoot.demo.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("c")
public class ConfigurationPageController {
	
	@Value("${test.student.name}")
	private String name;
	
	@Value("${test.class.id}")
	private String id;
	
	@Value("${test.teacher.age}")
	private String age;
	
	
	@RequestMapping("value")
	public String show(ModelMap model) {
		model.addAttribute("name", name);
		model.addAttribute("id", id);
		model.addAttribute("age", age);
		return "config";
	}
}

需要在启动类中配置加入:@PropertySource 

@PropertySource("classpath:config.properties") : 把config.properties 文件加载到spring容器中

package com.SpringBoot.demo;

import org.springframework.boot.Banner.Mode;
import org.springframework.boot.ResourceBanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Resource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;

@SpringBootApplication
@PropertySource("classpath:config.properties")
public class StudentTestApplication {

	public static void main(String[] args) {
		SpringApplication.run(StudentTestApplication.class, args);
	}

}

 

返回前端的界面:

config.html:接受值

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>This is a Configuration Test page</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	This is Configuration Test page!
	<h4>name:</h4><p th:text="${name}"></p>
	<h4>id:</h4><p th:text="${id}"></p>
	<h4>age:</h4><p th:text="${age}"></p>
	

</body>
</html>

 

测试:http://localhost:8008/SpringBoot/c/value

我们在配置文件中配置了其他端口8008 , 和项目名SpringBoot . 读取配置文件中的内容

 

五. 自定义的配置文件中有很多属性 的处理

如果一个配置文件中,有很多的属性,那么我们可以通过一个bean来储存数据内容

比如,上述的配置文件,我们也可以通过bean来装配数据

配置文件:

config.properties:

test.teacher.name=zhm
test.teacher.id=one
test.teacher.age=25

用bean来装配上述数据内容:

TESTConfigBean.java:

@ConfigurationProperties(prefix="test.teacher") 解析:配置下properties的内容前缀

package com.SpringBoot.demo.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="test.teacher")
public class TESTConfigBean {
	private String name;
	private String id;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	

}

那么,我们可以直接在启动类中调用这装配bean文件,会自动去加载配置文件到spring容器中。

StudentTestApplication.java : 

package com.SpringBoot.demo;

import org.springframework.boot.Banner.Mode;
import org.springframework.boot.ResourceBanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Resource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;

import com.SpringBoot.demo.configuration.TESTConfigBean;

@SpringBootApplication
@PropertySource("classpath:config.properties")
@EnableConfigurationProperties({TESTConfigBean.class})
public class StudentTestApplication {

	public static void main(String[] args) {
		SpringApplication.run(StudentTestApplication.class, args);

	}

}

既然,都把bean文件配置到了spring中,那么我们可以在其他类中直接注入就可以使用了。

package com.SpringBoot.demo.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("c")
public class ConfigurationPageController {
	
	@Value("${test.teacher.name}")
	private String name;
	
	@Value("${test.teacher.id}")
	private String id;
	
	@Value("${test.teacher.age}")
	private String age;
	
	
	@RequestMapping("value")
	public String show(ModelMap model) {
		model.addAttribute("name", name);
		model.addAttribute("id", id);
		model.addAttribute("age", age);
		return "config";
	}
}

我们需要一个测试的界面:config.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>This is a Configuration Test page</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	This is Configuration Test page!
	<h4>name:</h4><p th:text="${name}"></p>
	<h4>id:</h4><p th:text="${id}"></p>
	<h4>age:</h4><p th:text="${age}"></p>
	
	<h4>testConfigBean.name:</h4><p th:text="${testConfigBean.name}"></p>
	<h4>testConfigBean.id:</h4><p th:text="${testConfigBean.id}"></p>
	<h4>testConfigBean.age:</h4><p th:text="${testConfigBean.age}"></p>
</body>
</html>

 

运行测试: http://localhost:8008/SpringBoot/c/value

 

六. 配置文件中的 参数间的引用

如果想引用其他两个参数的值:config.properties:

test.teacher.name=zhm
test.teacher.id=one
test.teacher.age=25
test.teacher.show=${test.teacher.name}--${test.teacher.id}

 

那么我们只需要修改下bean类:TESTConfigBean.java

package com.SpringBoot.demo.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="test.teacher")
public class TESTConfigBean {
	private String name;
	private String id;
	private String age;
	
	private String show;
	
	
	
	public String getShow() {
		return show;
	}
	public void setShow(String show) {
		this.show = show;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	

}

然后在html界面显示:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>This is a Configuration Test page</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	This is Configuration Test page!
	<h4>name:</h4><p th:text="${name}"></p>
	<h4>id:</h4><p th:text="${id}"></p>
	<h4>age:</h4><p th:text="${age}"></p>
	
	<h4>testConfigBean.name:</h4><p th:text="${testConfigBean.name}"></p>
	<h4>testConfigBean.id:</h4><p th:text="${testConfigBean.id}"></p>
	<h4>testConfigBean.age:</h4><p th:text="${testConfigBean.age}"></p>
	<h4>testConfigBean.show:</h4><p th:text="${testConfigBean.show}"></p>
</body>
</html>

显示效果:

 

七.配置随机值

比如,引用在秘钥,或者返回key值,配置在文件中。

config.properties:

test.teacher.name=zhm
test.teacher.id=one
test.teacher.age=25
test.teacher.show=${test.teacher.name}--${test.teacher.id}

#random
myboot.random.secret=${random.value}
myboot.random.number=${random.int}
myboot.random.bigNumber=${random.long}
myboot.random.uuid=${random.uuid}
myboot.random.ten=${random.int(10)}
myboot.random.range=${random.int[1024,65536]}

采用bean来装配:

RandomConfigBean.java:

package com.SpringBoot.demo.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="myboot.random")
public class RandomConfigBean {
	private String secret;
	private int number;
	private long bigNumber;
	private String uuid;
	private int ten;
	private int range;
	public String getSecret() {
		return secret;
	}
	public void setSecret(String secret) {
		this.secret = secret;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public long getBigNumber() {
		return bigNumber;
	}
	public void setBigNumber(long bigNumber) {
		this.bigNumber = bigNumber;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public int getTen() {
		return ten;
	}
	public void setTen(int ten) {
		this.ten = ten;
	}
	public int getRange() {
		return range;
	}
	public void setRange(int range) {
		this.range = range;
	}


	
	
}

应用配置文件中的数据: 只需要@Autowired 自动注入即可

package com.SpringBoot.demo.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("c")
public class ConfigurationPageController {
	
	@Value("${test.teacher.name}")
	private String name;
	
	@Value("${test.teacher.id}")
	private String id;
	
	@Value("${test.teacher.age}")
	private String age;
	
	@Autowired
	private TESTConfigBean testConfigBean;
	
	@Autowired
	private RandomConfigBean randomConfigBean;
	
	
	@RequestMapping("value")
	public String show(ModelMap model) {
		model.addAttribute("name", name);
		model.addAttribute("id", id);
		model.addAttribute("age", age);
		model.addAttribute("testConfigBean", testConfigBean);
		model.addAttribute("randomConfigBean", randomConfigBean);
		return "config";
	}
}

 

 

同时也需要把bean装配到spring容器中:

package com.SpringBoot.demo;

import org.springframework.boot.Banner.Mode;
import org.springframework.boot.ResourceBanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Resource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;

import com.SpringBoot.demo.configuration.RandomConfigBean;
import com.SpringBoot.demo.configuration.TESTConfigBean;

@SpringBootApplication
@PropertySource("classpath:config.properties")
@EnableConfigurationProperties({TESTConfigBean.class,RandomConfigBean.class})
public class StudentTestApplication {

	public static void main(String[] args) {
		SpringApplication.run(StudentTestApplication.class, args);

	}

}

 

返回到的显示页面:

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>This is a Configuration Test page</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	This is Configuration Test page!
	<h4>name:</h4><p th:text="${name}"></p>
	<h4>id:</h4><p th:text="${id}"></p>
	<h4>age:</h4><p th:text="${age}"></p>
	
	<h4>testConfigBean.name:</h4><p th:text="${testConfigBean.name}"></p>
	<h4>testConfigBean.id:</h4><p th:text="${testConfigBean.id}"></p>
	<h4>testConfigBean.age:</h4><p th:text="${testConfigBean.age}"></p>
	<h4>testConfigBean.show:</h4><p th:text="${testConfigBean.show}"></p>
	
	<h4>----------------------</h4>
	
	<p>Following is random value :</p>
	<p th:text="${'secret:'+randomConfigBean.secret}"></p>
	<p th:text="${'number:'+randomConfigBean.number}"></p>
	<p th:text="${'bigNumber:'+randomConfigBean.bigNumber}"></p>
	<p th:text="${'uuid:'+randomConfigBean.uuid}"></p>
	<p th:text="${'ten'+randomConfigBean.ten}"></p>
	<p th:text="${'range'+randomConfigBean.range}"></p>
</body>
</html>

测试结果:

 

 

 

 

 

 

这篇关于SpringBoot第五章: application.properties 配置文件功能的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!