Spring Boot中配置文件application.properties里面配置项的引用

方法1:绑定对象bean调用

1、在application.properties或者application.yml里面添加我们的配置项:

2、创建一个配置类(该类和我们上面配置项的属性一一对应):

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

@Component
@ConfigurationProperties(prefix = "upload")
public class PropertiesConfig {
    private  int port;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

3、在需要调用的地方自动装配上我们上面的类并按照下面的方式获得配置项的值:

@Controller
@RequestMapping("/update/send")
public class UpdateSendController extends BaseController {
@Autowired
    private PropertiesConfig propertiesconfig;
    int port = propertiesconfig.getPort();
}

原文地址:https://www.cnblogs.com/luzhanshi/p/12093903.html

时间: 2024-08-02 21:00:15

Spring Boot中配置文件application.properties里面配置项的引用的相关文章

Spring Boot之配置文件application.properties

Spring Boot默认的配置文件为application.properties,放在src/main/resources目录下或者类路径的/config目录下,作为Spring Boot的全局配置文件,对一些默认的配置进行修改. 接下来使用例子展示Spring Boot配置文件的用法: 首先在src/main/resources下新建application.properties文件,内容如下 author.name=微儿博客 author.sex=男 创建controller类 ackage

【spring boot】配置文件 application.properties 属性解析

1.JPA命名策略 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy 有两种值可以配置分别为: 第一:org.hibernate.cfg.DefaultNamingStrategy 第二:org.hibernate.cfg.ImprovedNamingStrategy DefaultNamingStrategy这个直接映射,不会做过多的处理(前提没有设置@Table,@Column等属性的

spring boot 无法读取application.properties问题

spring boot 无法读取application.properties问题 https://bbs.csdn.net/topics/392374488 https://www.cnblogs.com/mr-wuxiansheng/p/6891925.html 关于spring获取webApplication.getBean多种途径和简单解释 https://blog.csdn.net/superdog007/article/details/43482427?readlog 原文地址:htt

Spring Boot2 系列教程(四)理解Spring Boot 配置文件 application.properties

在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,yaml 更加简洁明了,而且使用的场景也更多,很多开源项目都是使用 yaml 进行配置(例如 Hexo).除了简洁,yaml 还有另外一个特点,就是 yaml 中的数据是有序的,properties 中的数据是无序的,在一些需要路径匹配的配置中,顺序就显得尤为重要(例如我们在 Spring Cloud

spring boot 读取配置文件(application.yml)中的属性值111

在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: [html] view plain copy <!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-proc

spring boot中的底层配置文件application.yam(application.property)的装配原理初探

*在spring boot中有一个基础的配置文件application.yam(application.property)用于对spring boot的默认设置做一些改动. *在spring boot中有集成很多其他的包或者框架,如redis的操作的包,日志的等等. *在spring boot程序启动的时候,也就是下面这个类: @SpringBootApplicationpublic class Springboot1Application { public static void main(S

Spring Boot属性配置文件详解

自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: com.example.blog.name=zzh com.example.blog.title=hello springboot @Component public class BlogProperties { @Value("${com.example.blog.name}") private String name; @Value("${com.exa

spring-boot实战【12】:Spring Boot中使用JavaMailSender发送邮件

相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件. 快速入门 在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖: 1 2 3 4 <dependency> <groupId>org.springframew

spring-boot实战【08】【转】:Spring Boot属性配置文件详解

相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties中完成一些属性配置就能开启各模块的应用. 在之前的各篇文章中都有提及关于application.pro