`

Spring @ConfigurationProperties

阅读更多
1.配置文件类
package chengf.spring.boot.config.test;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Hello world!
 *
 */
@ConfigurationProperties
public class AppBootConfig {
	private String name;

	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}



2.业务代码引用
package chengf.spring.boot.config.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(AppBootConfig.class)
public class SampleApplication implements CommandLineRunner {

	@Autowired
	private AppBootConfig appBootConfig;
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(SampleApplication.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		System.out.println(appBootConfig.getName());
		
	}
	
}



配置文件
name=chengf
age=20


执行结果



证明正确将数据注入到bean中了
  • 大小: 13.7 KB
分享到:
评论

相关推荐

    Spring Boot技术知识点:如何理解@ConfigurationProperties注解

    在Spring Boot框架中,`@ConfigurationProperties`注解是一个核心特性,它允许我们将配置属性从外部化配置文件(如application.properties或application.yml)绑定到Java对象的字段上,从而简化了属性管理。...

    SpringBoot @ConfigurationProperties使用详解(源代码)

    在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景。本篇文章带大家了解一下三种场景的使用情况。 1.2 场景一 使用@ConfigurationProperties和@...

    @ConfigurationProperties注解使用方法(源代码)

    @ConfigurationProperties注解使用方法(源代码) 目录 @ConfigurationProperties注解使用方法 前言 一、Spring配置方式 1.1 第一阶段:xml配置 1.2 第二阶段:注解配置 1.3 第三阶段:Java配置(java config) 二、@...

    Spring Boot2.0 @ConfigurationProperties使用详解

    "Spring Boot 2.0 @ConfigurationProperties 使用详解" 概述 {@ConfigurationProperties} 是 Spring Boot 2.0 中的一个核心功能,用于简化外部化配置方式的使用。通过使用 {@ConfigurationProperties},可以轻松地...

    @ConfigurationProperties绑定配置信息至Array、List、Map、Bean的实现

    @ConfigurationProperties是Spring Boot提供的一种强大的特性,用于将配置文件(如application.properties或application.yml)中的键值对映射到Java对象的属性上。这使得我们可以更方便地管理和使用配置信息,尤其是...

    软件框架技术-使用@Component@ConfigurationProperties等方法实现将配置文件的注入,并在控制台显示

    在本文中,我们将深入探讨如何在Java Spring框架中利用`@Component`和`@ConfigurationProperties`注解将配置文件中的属性注入到bean中,并在控制台进行显示。这对于理解和掌握Spring框架的应用以及如何处理配置文件...

    SpringBoot @ConfigurationProperties使用详解

    在Spring Boot中,`@ConfigurationProperties`是用于将配置文件中的属性绑定到Java Bean的注解,极大地简化了属性的管理,使得配置更加灵活和类型安全。以下是对`@ConfigurationProperties`的详细解析。 **1. 添加...

    Spring @compenent注解详解

    在Spring框架中,`@Component`注解是核心的组件注解之一,它标志着一个类作为Spring IoC容器中的Bean。这个注解及其派生注解(如`@Service`、`@Repository`和`@Controller`)是Spring依赖注入(Dependency Injection...

    spring boot 使用 ConfigurationProperties.docx

    在Spring Boot中,`@ConfigurationProperties` 是一个强大的特性,用于将配置文件(如application.properties或application.yml)中的键值对映射到Java对象的属性上。这使得开发者能够更方便地管理和使用配置信息,...

    SpringBoot系列之 PropertySource用法的简介.docx

    `@PropertySource`在Spring Boot中,通常与`@ConfigurationProperties`结合使用,以实现更高级别的属性绑定和类型安全的属性注入。 `@PropertySource`注解的使用方法如下: 1. **定义**: `@PropertySource`用来...

    Testing_Configuration_Properties:一个简单的设置来测试Spring Boot的@ConfigurationProperties

    在Spring Boot中测试@ConfigurationProperties 该项目显示了如何在Spring Boot中测试@ConfigurationProperties。 您可以在以下位置找到有关此内容的博客文章:

    Spring@注解.md

    ### Spring 注解详解 #### 一、Bean概念与作用 - **Bean的含义**:在Spring框架中,Bean可以理解为任何需要实例化的类。它不仅限于JavaBean规范所描述的那种具有公共getter和setter方法的类,而是泛指任何需要在...

    Spring boot将配置属性注入到bean类中

    import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component @...

    Spring@PropertySource 配置文件自定义加密、自定义Yaml文件加载

    3. **`@ConfigurationProperties`**:Spring Boot提供`@ConfigurationProperties`注解,可以将YAML中的键值对映射到Java对象。创建对应的Java类,使用这个注解,并指定YAML文件的前缀。 4. **注册`PropertySource`**...

    Spring注解 - 52注解 - 原稿笔记

    @ConfigurationProperties , @Controller , @CrossOrigin , @EnableWebMvc , @GetMapping , @Import , @JsonDeserialize , @JsonIgnore , @JsonIgnoreProperties , @JsonIgnoreType , @JsonInclude , @JsonProperty...

    详解关于SpringBoot的外部化配置使用记录

    @ConfigurationProperties用来将配置文件中的值映射到Java配置类中,而@Configuration用来将Java配置类注册到Spring容器中。 在使用@ConfigurationProperties时,需要在Java配置类中提供Setter方法,以便SpringBoot...

    spring4.0引用properties

    `PropertyPlaceholderConfigurer`是Spring早期版本中用于注入properties文件中值的bean,而`@ConfigurationProperties`是Spring Boot引入的,更适合现代Spring应用。 使用`PropertyPlaceholderConfigurer`的例子...

    SpringCloud实用篇02

    SpringCloud实用篇02 本篇讲解了SpringCloud实用篇02,主要介绍了Nacos配置管理的使用方法和实现原理。Nacos是阿里巴巴开源的配置管理系统,能够提供统一的配置管理、配置热更新、配置共享等功能。 Nacos配置管理 ...

    spring02-6

    Spring通过`@Value`注解和`PropertyPlaceholderConfigurer`(在Spring 3.1以后被`@ConfigurationProperties`替代)等方式来实现属性注入。下面,我们将深入探讨这一概念及其应用。 首先,让我们了解什么是属性文件...

    Spring Boot最常用的30个注解.docx

    5 @ConfigurationProperties 四、 HTTP请求相关 1 @GetMapping 2 @PostMapping 3 @RequestMapping 五、 前后端传值相关 1 @PathVariable 2 @RequestParam 3 @RequestBody 4 @Responsebody 六、 全局异常处理相关 1 @...

Global site tag (gtag.js) - Google Analytics