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

 更新时间:2021年09月10日 16:38:53   作者:记忆储存处  
这篇文章主要介绍了SpringBoot获取application.properties中的自定义的值,目录结构文件代码给大家列举的非常详细,需要的朋友可以参考下

目录结构:

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;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPassword=" + userPassword + ", laBigDecimal="
				+ laBigDecimal + ", maps=" + maps + ", lists=" + lists + ", department=" + department + "]";
	}
 
}

I18nSpringbootDemo1Application类:

package com.example.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * 应用启动类
 *
 */
@SpringBootApplication
public class I18nSpringbootDemo1Application {
 
	public static void main(String[] args) {
		SpringApplication.run(I18nSpringbootDemo1Application.class, args);
	}
 
}

单元测试类I18nSpringbootDemo1ApplicationTests:

package com.example.demo;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
import com.example.demo.obj.User;
 
@SpringBootTest
class I18nSpringbootDemo1ApplicationTests {
	@Autowired
	User user;
	
	@Test
	void contextLoads() {
		System.out.println(user.toString());
	}
 
}

 启动:

结果:
User [id=12, userName=zhansan, userPassword=PWD123, laBigDecimal=138.3, maps={key1=V1, key2=123}, lists=[a12, a13, sdf], department=Department [depCode=dep001, depName=depName001]]

到此这篇关于SpringBoot获取application.properties中的自定义的值的文章就介绍到这了,更多相关SpringBoot获取自定义值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 聊聊SpringCloud中的Ribbon进行服务调用的问题

    聊聊SpringCloud中的Ribbon进行服务调用的问题

    SpringCloud-Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。本文给大家介绍SpringCloud中的Ribbon进行服务调用的问题,感兴趣的朋友跟随小编一起看看吧
    2022-01-01
  • SpringBoot 创建web项目并部署到外部Tomcat

    SpringBoot 创建web项目并部署到外部Tomcat

    本篇文章主要介绍了SpringBoot 创建web项目并部署到外部Tomcat,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • SpringBoot熔断机制之CircuitBreaker详解

    SpringBoot熔断机制之CircuitBreaker详解

    这篇文章主要介绍了SpringBoot熔断机制之CircuitBreaker详解,SpringBoot的熔断机制在微服务架构中扮演着重要角色,其中CircuitBreaker是其核心机制之一,用于防止服务的异常状态影响到整个系统的运作,需要的朋友可以参考下
    2023-10-10
  • Java如何跳过https的ssl证书验证详解

    Java如何跳过https的ssl证书验证详解

    这篇文章主要介绍了Java跳过https的ssl证书验证的解决思路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面我们来深入学习下吧
    2019-06-06
  • java图片对比度调整示例代码

    java图片对比度调整示例代码

    这篇文章主要给大家介绍了关于java图片对比度调整的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • 学习Java之自定义异常与NullPointerException的处理

    学习Java之自定义异常与NullPointerException的处理

    有时候Java自身提供的异常类并不能很好地表达我们的需求,所以这时候我们就可以自定义异常,也就是说,我们可以制造出一个自己的异常类,这样就可以抛出或捕获自己的异常了,本文就给大家详细讲讲Java自定义异常与NullPointerException的处理
    2023-08-08
  • Jenkins环境搭建之下载与安装过程

    Jenkins环境搭建之下载与安装过程

    Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,集成Jenkins可以用于一些测试和部署技术,对Jenkins环境搭建之下载与安装过程感兴趣的朋友跟随小编一起看看吧
    2021-12-12
  • 浅谈关于Java的GC垃圾回收器的一些基本概念

    浅谈关于Java的GC垃圾回收器的一些基本概念

    这篇文章主要介绍了关于Java的GC垃圾回收器的一些基本概念,牵扯倒JVM内存模型的一些知识,需要的朋友可以参考下
    2015-11-11
  • Java 三种进制的数值常量操作

    Java 三种进制的数值常量操作

    这篇文章主要介绍了Java 三种进制的数值常量操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • java中BCryptPasswordEncoder密码的加密与验证方式

    java中BCryptPasswordEncoder密码的加密与验证方式

    这篇文章主要介绍了java中BCryptPasswordEncoder密码的加密与验证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08

最新评论