• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot如何获取application.properties中自定义的值

springboot 搞代码 4年前 (2022-01-09) 34次浏览 已收录 0个评论

目录结构:

pom文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>i18nSpringbootDemo-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>i18nSpringbootDemo-1</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		
		<!--校验依赖-->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
	<packaging>war</packaging>
</project>

 application.properties:

test.user.id=12
#也可以写成 test.user.user-name=zhangsan
test.user.userName=zhansan
#也可以写成 test.user.user-password=XXX
test.user.userPassword=PWD123
#也可以写成 test.user.la-big-decimal=XXX
test.user.laBigDecimal=138.3
test.user.maps.key1=V1
test.user.maps.key2=123
test.user.lists=a12,a13,sdf
test.user.department.dep-code=dep001
test.user.department.dep-name=depName001

Department类:

package com.example.demo.obj;
 
public class Department {
	private String depCode;
	private String depName;
	
	/**
	 * @return depCode
	 */
	public String getDepCode() {
		return depCode;
	}
	/**
	 * @param depCode セットする depCode
	 */
	public void setDepCode(String depCode) {
		this.depCode = depCode;
	}
	/**
	 * @return depName
	 */
	public String getDepName() {
		return depName;
	}
	/**
	 * @param depName セットする depName
	 */
	public void setDepName(String depName) {
		this.depName = depName;
	}
	@Override
	public String toString() {
		return "Department [depCode=" + depCode + ", depName=" + depName + "]";
	}
	
}

User类:

package com.example.demo.obj;
 
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
 
import javax.validation.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
 
/*
 * 将配置文件的每一个属性值,映射到这个组件中:
 * ①@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 *     prefix = "test.user":将配置文件中前缀为test.user下面的所有属性进行一一映射
 *  只有这个组件是容器中的组件,才能提供@ConfigurationProperties的功能,所以要加@Component
 * 
 * ②@Value("${key}")从环境变量、配置文件中获取值
 * @Value("#{SpEl}")表达式
 * 
 * @ConfigurationProperties与@Value的区别:
 * @ConfigurationProperties支持松散语法,JSR303数据校验,复杂类型封装,不支持SpEL
 * @Value支持SpEL,不支持松散语法,JSR303数据校验,复杂类型封装
 * 如果说,我们在某个业务逻辑中需要获取一下配置文件中的某项值,可以用@Value
 * 如果说,我们专门编写了一个javaBean去和配置文件进行映射,我们直接使用@ConfigurationProperties
 */
@Component
@ConfigurationProperties(prefix = "test.user")
@Validated
public class User {
	//@Value("#{10*2}")
	private Integer id;
	//@Email userName必须输入邮箱格式的值,要不然报错
	//@Value("${test.user.userName}")
	private String userName;
	//@Value("${test.user.userPassword}")
	private String userPassword;
	//@Value("${test.user.laBigDecimal}")
	private BigDecimal laBigDecimal;
	
	//@Value("${test.user.maps}") X 不行会报错
	private Map<String, Object> maps;
	//@Value("${test.user.lists}")
	private List<Object> lists;
	//@Value("${test.user.department}") X 不行会报错
	private Department department;
	
	/**
	 * @return id
	 */
	public Integer getId() {
		return id;
	}
	/**
	 * @param id セットする id
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	/**
	 * @return userName
	 */
	public String getUserName() {
		return userName;
	}
	/**
	 * @param userName セットする userName
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * @return userPassword
	 */
	public String getUserPassword() {
		return userPassword;
	}
	/**
	 * @param userPassword セットする userPassword
	 */
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	/**
	 * @return laBigDecimal
	 */
	public BigDecimal getLaBigDecimal() {
		return laBigDecimal;
	}
	/**
	 * @param laBigDecimal セットする laBigDecimal
	 */
	public void setLaBigDecimal(BigDecimal laBigDecimal) {
		this.laBigDecimal = laBigDecimal;
	}
	/**
	 * @return maps
	 */
	public Map<String, Object> getMaps() {
		return maps;
	}
	/**
	 * @param maps セットする maps
	 */
	public void setMaps(Map<String, Object> maps) {
		this.maps = maps;
	}
	/**
	 * @return lists
	 */
	public List<Object> getLists() {
		return lists;
	}
	/**
	 * @param lists セットする lists
	 */
	public void setLists(List<Object> lists) {
		this.lists = lists;
	}
	/**
	 * @return department
	 */
	public Department getDepartment() {
		return department;
	}
	/**
	 * @param department セットする department
	 */
	public void setDepartment(Department department) {
		this.department = department;
	}
	@Overr<div style="color:transparent">本文来源gaodai.ma#com搞#代!码(网</div>ide
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPassword=" + userPassword + ", laBigDecimal="
				+ laBigDecimal + ", maps=" + maps + ", lists=" + lists + ", department=" + department + "]";
	}
 
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot如何获取application.properties中自定义的值
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址