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获取自定义值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中的transient关键字解析

    Java中的transient关键字解析

    这篇文章主要介绍了Java中的 transient关键字解析,有时候我们的一些敏感信息比如密码并不想序列化传输给对方,这个时候transient关键字就派上用场了,如果一个类的变量加上了transient关键字那么这个字段就不会被序列化,需要的朋友可以参考下
    2023-09-09
  • 详解JVM如何判断一个对象是否可以被回收

    详解JVM如何判断一个对象是否可以被回收

    在c++中,当我们使用完某个对象的时候,需要显示的将对象回收,在java中,jvm会帮助我们进行垃圾回收,无需程序员自己写代码进行回收,下面我们就来看看JVM是如何判断一个对象是否可以被回收的吧
    2023-11-11
  • Spring 处理 HTTP 请求参数注解的操作方法

    Spring 处理 HTTP 请求参数注解的操作方法

    这篇文章主要介绍了Spring 处理 HTTP 请求参数注解的操作方法,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友参考下吧
    2024-04-04
  • SpringBoot如何打印mybatis的执行sql问题

    SpringBoot如何打印mybatis的执行sql问题

    这篇文章主要介绍了SpringBoot如何打印mybatis的执行sql问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Mapstruct对象插入数据库某个字段总是为空的bug详解

    Mapstruct对象插入数据库某个字段总是为空的bug详解

    这篇文章主要为大家介绍了在一次需求开发Mapstruct中对象插入数据库某个字段总是为空的bug问题详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Java最长公共子序列示例源码

    Java最长公共子序列示例源码

    这篇文章主要介绍了Java最长公共子序列的定义及示例源代码,具有一定参考价值,需要的朋友可以看下。
    2017-09-09
  • Java利用Socket类实现TCP通信程序

    Java利用Socket类实现TCP通信程序

    TCP通信能实现两台计算机之间的数据交互,通信的两端,要严格区分为客户端与服务端,下面我们就来看看Java如何利用Socket类实现TCP通信程序吧
    2024-02-02
  • 详细介绍SpringCloud之Ribbon

    详细介绍SpringCloud之Ribbon

    本篇文章主要介绍了SpringCloud之Ribbon,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • 如何修改HttpServletRequest中header中的信息

    如何修改HttpServletRequest中header中的信息

    这篇文章主要介绍了如何修改HttpServletRequest中header中的信息,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Spring Boot 实现图片上传并回显功能

    Spring Boot 实现图片上传并回显功能

    本篇文章给大家分享Spring Boot 实现图片上传并回显功能,文中通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-07-07

最新评论