首页 > 其他分享 >springboot/spring使用ConfigurationProperties注解读取自定义属性(尚硅谷)

springboot/spring使用ConfigurationProperties注解读取自定义属性(尚硅谷)

时间:2022-10-13 22:06:39浏览次数:47  
标签:自定义 spring boot springframework ConfigurationProperties import org lombok zhangs

pom.xml

<?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.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.atguigu</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>上硅谷spring boot学习</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>1.18.12</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!--将应用打包成一个可执行的jar包,生成后放到target目录-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--
-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。

-Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。
-->
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
DemoApplicationTests.java
package com.atguigu;

import com.atguigu.bean.Person;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class DemoApplicationTests {


@Autowired
Person person;

@Test
void customProperties()
{
log.info("{}",person);
}
}

application.yml(lastName等价于last-name)

debug: true
server:
port: 8080
person:
lastName: zhangsan
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 'zhangsan \nlisi',k3: "zhangsan \nlisi"}
lists:
- lisi
- zhaoliu
dog:
name: happy
age: 5
等价的application.properties
debug=true
person.lastName=zhangsan
person.age=18
person.boss=false
person.birth=2017/12/12
person.maps.k1=v1
person.maps.k2='zhangsan \nlisi'
person.maps.k3="zhangsan \nlisi"
person.lists=[lisi, zhaoliu]
person.dog.name=happy
person.dog.age=5
server.port=8080
DemoApplication.java
package com.atguigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Dog.java
package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
private String name;
private Integer age;
}
Person.java
package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;

private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
Dog.java
package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
private String name;
private Integer age;
}

输出结果:

2020-04-04 20:31:27.858  INFO 1736 --- [           main] com.atguigu.DemoApplicationTests         : Person(lastName=zhangsan, age=18, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=zhangsan \nlisi, k3=zhangsan 
lisi}, lists=[lisi, zhaoliu], dog=Dog(name=happy, age=5))

springboot/spring使用ConfigurationProperties注解读取自定义属性(尚硅谷)_maven

 

""双引号不会转义特殊字符, name:"zhangsan \n lisi" zhangsan 换行 lisi
'' 单引号转义特殊字符 name : 'zhangsan \n lisi' 显示 'zhangsan \n lisi'

下篇:SpringBoot/Spring使用@Value进行属性绑定(传智播客代码)

标签:自定义,spring,boot,springframework,ConfigurationProperties,import,org,lombok,zhangs
From: https://blog.51cto.com/u_296714/5754706

相关文章

  • SpringBoot集成JMH进行基准测试
    基准测试时评估程序/接口性能的一个有效的方法。笔者早期在网上了解到​​JMH​​这个代码工具。由于之前找到的一些代码不具备项目实践场景的说服力,通常是一项简单算法,独立......
  • SpringMVC中实现Bean Validation(JSR 303 JSR 349 JSR 380)
    JSR303是针对bean数据校验提出的一个规范。使用注解方式实现数据校验。每个注解的用法这里就不多介绍,请移步​​JSR303-BeanValidation介绍及最佳实践​​笔者上面提......
  • org.springframework.web.util.Log4jWebConfigurer
    org.springframework.web.util.Log4jWebConfigurer@Deprecated Deprecated.asofSpring4.2.1,infavorofApacheLog4j2(followingApache'sEOLdeclarationfor......
  • Spring boot笔记4
    减少配置修改次数方便环境配置切换application.yml#默认加载的配置文件spring:profiles:active:prod application-dev.ymlapplication-prod.yml......
  • 几行代码,搞定 SpringBoot 接口恶意刷新和暴力请求!
    在实际项目使用中,必须要考虑服务的安全性,当服务部署到互联网以后,就要考虑服务被恶意请求和暴力攻.击的情况,下面的教程,通过​​intercept​​和​​redis​​针对​​url+ip......
  • 教你优雅的实现 SpringBoot 并行任务
    SpringBoot的定时任务:第一种:把参数配置到.properties文件中:代码:packagecom.accord.task;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.spring......
  • Spring boot 笔记
    JSR303配置属性值的数据校验hibernate-validator        @Validated必须在主类上标注可以校验所有子类的所有属性@NotEmpty       ......
  • SpringBoot深入理解
    当使用打包时,会下载org-springframework-boot-loader的jar,并且不会放在lib存放的第三方jar包文件中,该jar包中有个JarLauncher.class文件中设置了jar包运行时的入口和打包后......
  • Spring实战笔记二(bean的作用域、运行时注入、)
    一、bean的作用域      默认情况下,Spring应用上下文中所有bean都是以单例(singleton)的形式创建的。      Spring定义的多种作用域,可以基于这些作用域创建be......
  • 自定义注解
    1.注解介绍1.注解是一种元数据(任何文件系统中的数据分为数据和元数据。数据是指普通文件中实际数据,而元数据只用来描述一个文件特征的系统数据,例如访问权限等)形式,即......