iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot属性配置中获取值的方式是什么
  • 647
分享到

SpringBoot属性配置中获取值的方式是什么

2023-06-29 04:06:30 647人浏览 泡泡鱼
摘要

这篇文章主要介绍“SpringBoot属性配置中获取值的方式是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springBoot属性配置中获取值的方式是什么”文章能帮助大家解决问题。Spring

这篇文章主要介绍“SpringBoot属性配置中获取值的方式是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springBoot属性配置中获取值的方式是什么”文章能帮助大家解决问题。

SpringBoot 属性配置中获取值

在配置文件中定义属性的值,然后再获取,在这里做一个总结,首先,在application.yml文件中定义端口和属性的值,当然,在.application.properties文件中也能定义,只是格式不同而已:

appliaction.yml:

server:  port: 8081cubSize: Bage: 18

然后再定义一个controller,用@Value这个注解来获取到值:

package com.dist.tr.controller;import com.dist.tr.entity.GrilProperties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.WEB.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMappingpublic class BeautifulGirlContrller {    @Value("${cubSize}")    private String cubSize;    @Value("${age}")    private Integer age;    @RequestMapping(value = "/gril",method = RequestMethod.GET)    public String HelloGril(){        return cubSize + age;    }}

运行结果:

SpringBoot属性配置中获取值的方式是什么

如果当属性很多之后,要写很多的@Value 的注解嘛???答案当然是No,有简便的写法:

application.yml 文件

server:  port: 8081gril:  name: lisa  height: 165

首先,定义一个实体类去写属性

GrilProperties实体:

注意我们用到了这个注解:@ConfigurationProperties(prefix = “gril”)

package com.dist.tr.entity;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "gril")public class GrilProperties {    private String name;    private String height;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getHeight() {        return height;    }    public void setHeight(String height) {        this.height = height;    }}

在controller 中的写法:

首先用注解@Autowired 注入这个实体,如果不在实体中加@Component这个注解,在idea中发现会有红线出现。

package com.dist.tr.controller;import com.dist.tr.entity.GrilProperties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMappingpublic class BeautifulGirlContrller {    @Autowired    private GrilProperties grilProperties;    @RequestMapping("/grilPerproties")    public String grilPerproties(){        return grilProperties.getName()+grilProperties.getHeight();    }}

运行结果:

SpringBoot属性配置中获取值的方式是什么

这样就不会需要去写太多的@Value注解了。

还有中形式,就是在配置文件中也可以有这种情况出现:

server:  port: 8081cubSize: Bage: 18context: "cubSize:${cubSize},age:${age}"

这种情况证明获取的属性值呢?

在controller中编码:

package com.dist.tr.controller;import com.dist.tr.entity.GrilProperties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMappingpublic class BeautifulGirlContrller {    @Value("${context}")    private String context;    @RequestMapping("/grilSize")    public String girlcubSize(){        return context ;    }}

运行结果:

SpringBoot属性配置中获取值的方式是什么

测试和生产区分

当在项目开发的时候,如果区分测试和生产环境,那么就得区分开application.yml 文件:

新建application-dev.yml 文件和application-prod.yml文件:

然后在使用测试或者是生产的时候,application.yml 文件中这样写:

spring:  profiles:    active: prod

决定是用测试环境还是生产环境。

SpringBoot 获取值和配置文件

@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean

1、@ConfigurationProperties(prefix = "student")方式

(1)定义两个实体类,其中student实体类的属性包括Course类:

@Data@Component@ConfigurationProperties(prefix = "student")//告诉springboot将本类中的所有属性和配置文件的相关配置进行绑定public class Student {       //prefix:配置文件中哪一个名称下面的属性进行一一映射    private String sname;    private int age;    private Map<String,Object> maps;    private List<Object> list;    private Course course;}@Datapublic class Course {    private String courseno;    private String coursename;}

(2)创建yaml配置文件:

student:  sname: zhai  age: 12  maps: {k1: 12,k2: 13}  list:    - zhai    - zhang  course:    courseno: 202007    coursename: javaweb

(3)创建properties文件:

#配置studentstudent.age=12student.sname=zhaistudent.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java

(4)测试类:

//可以在测试期间很方便地类似编码一样进行自动注入等容器的功能@SpringBootTestclass Springboot03ApplicationTests {    @Autowired    Student student;    @Test    void contextLoads() {        System.out.println(student);    }}

(5)需要导入的依赖:配置文件处理器,配置文件进行绑定会有提示

   <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-configuration-processor</artifactId>            <version>2.2.1.RELEASE</version>   </dependency>

SpringBoot属性配置中获取值的方式是什么

2、@Value方式

(1)书写配置文件

#配置studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java

(2)获取值:

@Data@Componentpublic class Student {    @Value("${student.sname}")    private String sname;    @Value("#{2*9}")    private int age;    private Map<String,Object> maps;    private List<Object> list;    private Course course;}

(3)@ConfigurationProperties(prefix = "")方式与@Value方式的比较

  • @ConfigurationProperties(prefix = "")方式支持批量注入配置文件的属性,@Value方式需要一个个指定

  • @ConfigurationProperties(prefix = "")方式支持松散绑定,@Value方式不支持,

yml中写的last-name,这个和lastName是一样的,后面跟着的字母默认是大写的。这就是松散绑定

@Value方式支持jsR303校验

@Data@Component@Validatedpublic class Student {    @NonNull    private String sname;    private int age;    private Map<String,Object> maps;    private List<Object> list;    private Course course;}

@Value方式支持SpEl

如果我们只是在某一个业务逻辑中需要获取配置文件的某一项值,可以使用@Value,如果是一个javaBean来和配置文件进行映射,则要使用@ConfigurationProperties(prefix = "")方式

@RestControllerpublic class HelloController {    @Value("${student.sname}")    private String sname;    @RequestMapping("/hello")    public String hello(){        return "hello"+sname;    }}

配置文件:

#配置studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java

3、@PropertySource

(1)配置文件(student.properties)

#配置studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java

(2)实体类获取值

@Data@Component@PropertySource(value = {"classpath:student.properties"})public class Student {    private String sname;    private int age;    private Map<String,Object> maps;    private List<Object> list;    private Course course;}

@PropertySource是从指定路径下获取数据,默认是从application.properties下获取数据

4、@ImportResource和@Bean

(1)指定spring的配置文件

@SpringBootApplication(scanBasePackages = "com")@ImportResource(locations = {"classpath:beans.xml"})public class Springboot02Application {    public static void main(String[] args) {        SpringApplication.run(Springboot02Application.class, args);    }}

(2)书写spring的配置文件:beans.xml

(3)书写如下配置,可以省略配置文件的书写,用注解来代替

@Configurationpublic class MyAppConfig {    @Bean    public HelloService helloService(){        return new HellService();    }}

@Configuration说明这是一个配置类,就是在替代之前的配置文件

@Bean标记在方法上,该方式将方法的返回值添加到容器中,容器中组件的ID默认是方法名

关于“SpringBoot属性配置中获取值的方式是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。

--结束END--

本文标题: SpringBoot属性配置中获取值的方式是什么

本文链接: https://www.lsjlt.com/news/322492.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot属性配置中获取值的方式是什么
    这篇文章主要介绍“SpringBoot属性配置中获取值的方式是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot属性配置中获取值的方式是什么”文章能帮助大家解决问题。Spring...
    99+
    2023-06-29
  • SpringBoot 属性配置中获取值的方式
    目录SpringBoot 属性配置中获取值首先,定义一个实体类去写属性测试和生产区分SpringBoot 获取值和配置文件1、@ConfigurationProperties(pre...
    99+
    2022-11-13
  • springboot获取properties属性值的多种方式总结
    目录获取properties属性值方式总结1. 除了默认配置在 application.properties的多环境中添加属性2. 使用之前在spring中加载的value值形式3....
    99+
    2022-11-13
  • java中field获取属性值的方法是什么
    在Java中,要获取字段(field)的属性值,可以使用以下几种方法:1. 使用字段的getter方法:如果字段有对应的getter...
    99+
    2023-09-25
    java
  • 使用springboot如何实现获取配置文件中的属性值
    本篇文章给大家分享的是有关使用springboot如何实现获取配置文件中的属性值,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。在spring boot中,简单几步,读取配置文件...
    99+
    2023-05-31
    springboot 配置文件
  • SpringBoot从配置文件中获取属性的四种方法总结
    目录方式一:@Value方式二:@ConfigurationProperties@Value和@ConfigurationProperties比较方式三:@PropertySourc...
    99+
    2022-11-13
  • springboot配置文件中属性变量引用的方式是什么
    本文小编为大家详细介绍“springboot配置文件中属性变量引用的方式是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“springboot配置文件中属性变量引用的方式是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来...
    99+
    2023-07-05
  • springboot yml配置文件值的注入方式是什么
    这篇文章跟大家分析一下“springboot yml配置文件值的注入方式是什么”。内容详细易懂,对“springboot yml配置文件值的注入方式是什么”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够...
    99+
    2023-06-26
  • PostgreSQL执行查询时获取元组属性值实现方法是什么
    本篇内容主要讲解“PostgreSQL执行查询时获取元组属性值实现方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PostgreSQL执行查询时获取元组...
    99+
    2022-10-18
  • jQuery中怎么为匹配元素设置样式属性的值
    小编给大家分享一下jQuery中怎么为匹配元素设置样式属性的值,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!jquery是什么jquery是一个简洁而快速的Jav...
    99+
    2023-06-14
  • springboot整合多数据源配置的方式是什么
    这篇文章将为大家详细讲解有关springboot整合多数据源配置的方式是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。简介主要介绍两种整合方式,分别是 springboot+mybati...
    99+
    2023-06-22
  • SpringBoot获取前台参数的方式及统一响应的方法是什么
    本篇内容介绍了“SpringBoot获取前台参数的方式及统一响应的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!请求SpringB...
    99+
    2023-07-05
  • nodejs项目中的package.json的常见配置属性是什么
    这篇文章主要讲解了“nodejs项目中的package.json的常见配置属性是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“nodejs项目中的pac...
    99+
    2022-10-19
  • mybatis-plus插入修改配置默认值的实现方式是什么
    这篇文章主要介绍“mybatis-plus插入修改配置默认值的实现方式是什么”,在日常操作中,相信很多人在mybatis-plus插入修改配置默认值的实现方式是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答...
    99+
    2023-07-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作