文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

目录结构:

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;
	
	
	public String getDepCode() {
		return depCode;
	}
	
	public void setDepCode(String depCode) {
		this.depCode = depCode;
	}
	
	public String getDepName() {
		return 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;
 

@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;
	
	
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getUserName() {
		return userName;
	}
	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String getUserPassword() {
		return userPassword;
	}
	
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	
	public BigDecimal getLaBigDecimal() {
		return laBigDecimal;
	}
	
	public void setLaBigDecimal(BigDecimal laBigDecimal) {
		this.laBigDecimal = laBigDecimal;
	}
	
	public Map<String, Object> getMaps() {
		return maps;
	}
	
	public void setMaps(Map<String, Object> maps) {
		this.maps = maps;
	}
	
	public List<Object> getLists() {
		return lists;
	}
	
	public void setLists(List<Object> lists) {
		this.lists = lists;
	}
	
	public Department getDepartment() {
		return 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获取自定义值内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯