经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot?属性配置中获取值的方式
来源:jb51  时间:2022/2/14 11:35:43  对本文有异议

SpringBoot 属性配置中获取值

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

appliaction.yml:

  1. server:
  2. port: 8081
  3. cubSize: B
  4. age: 18

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

  1. package com.dist.tr.controller;
  2. import com.dist.tr.entity.GrilProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping
  10. public class BeautifulGirlContrller {
  11. @Value("${cubSize}")
  12. private String cubSize;
  13. @Value("${age}")
  14. private Integer age;
  15. @RequestMapping(value = "/gril",method = RequestMethod.GET)
  16. public String HelloGril(){
  17. return cubSize + age;
  18. }
  19. }

运行结果:

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

application.yml 文件

  1. server:
  2. port: 8081
  3. gril:
  4. name: lisa
  5. height: 165

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

GrilProperties实体:

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

  1. package com.dist.tr.entity;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @ConfigurationProperties(prefix = "gril")
  6. public class GrilProperties {
  7. private String name;
  8. private String height;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getHeight() {
  16. return height;
  17. }
  18. public void setHeight(String height) {
  19. this.height = height;
  20. }
  21. }

在controller 中的写法:

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

  1. package com.dist.tr.controller;
  2. import com.dist.tr.entity.GrilProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping
  10. public class BeautifulGirlContrller {
  11. @Autowired
  12. private GrilProperties grilProperties;
  13. @RequestMapping("/grilPerproties")
  14. public String grilPerproties(){
  15. return grilProperties.getName()+grilProperties.getHeight();
  16. }
  17. }

运行结果:

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

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

  1. server:
  2. port: 8081
  3. cubSize: B
  4. age: 18
  5. context: "cubSize:${cubSize},age:${age}"

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

在controller中编码:

  1. package com.dist.tr.controller;
  2. import com.dist.tr.entity.GrilProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping
  10. public class BeautifulGirlContrller {
  11. @Value("${context}")
  12. private String context;
  13. @RequestMapping("/grilSize")
  14. public String girlcubSize(){
  15. return context ;
  16. }
  17. }

运行结果:

测试和生产区分

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

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

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

  1. spring:
  2. profiles:
  3. active: prod

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

SpringBoot 获取值和配置文件

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

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

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

  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = "student")//告诉springboot将本类中的所有属性和配置文件的相关配置进行绑定
  4. public class Student { ? ? ? //prefix:配置文件中哪一个名称下面的属性进行一一映射
  5. ? ? private String sname;
  6. ? ? private int age;
  7. ? ? private Map<String,Object> maps;
  8. ? ? private List<Object> list;
  9. ? ? private Course course;
  10. }
  11. @Data
  12. public class Course {
  13. ? ? private String courseno;
  14. ? ? private String coursename;
  15. }

(2)创建yaml配置文件:

  1. student:
  2. ? sname: zhai
  3. ? age: 12
  4. ? maps: {k1: 12,k2: 13}
  5. ? list:
  6. ? ? - zhai
  7. ? ? - zhang
  8. ? course:
  9. ? ? courseno: 202007
  10. ? ? coursename: javaweb

(3)创建properties文件:

  1. #配置student
  2. student.age=12
  3. student.sname=zhai
  4. student.maps.k1=1
  5. student.maps.k2=2
  6. student.list=a,b,c
  7. student.course.courseno=202007
  8. student.course.coursename=java

(4)测试类:

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

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

  1. ? ?<dependency>
  2. ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
  3. ? ? ? ? ? ? <artifactId>spring-boot-configuration-processor</artifactId>
  4. ? ? ? ? ? ? <version>2.2.1.RELEASE</version>
  5. ? ?</dependency>

2、@Value方式

(1)书写配置文件

  1. #配置student
  2. student.sname=zhai
  3. student.age=12
  4. student.maps.k1=1
  5. student.maps.k2=2
  6. student.list=a,b,c
  7. student.course.courseno=202007
  8. student.course.coursename=java

(2)获取值:

  1. @Data
  2. @Component
  3. public class Student {
  4. ? ? @Value("${student.sname}")
  5. ? ? private String sname;
  6. ? ? @Value("#{2*9}")
  7. ? ? private int age;
  8. ? ? private Map<String,Object> maps;
  9. ? ? private List<Object> list;
  10. ? ? private Course course;
  11. }

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

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

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

@Value方式支持JSR303校验

  1. @Data
  2. @Component
  3. @Validated
  4. public class Student {
  5. ? ? @NonNull
  6. ? ? private String sname;
  7. ? ? private int age;
  8. ? ? private Map<String,Object> maps;
  9. ? ? private List<Object> list;
  10. ? ? private Course course;
  11. }

@Value方式支持SpEl

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

  1. @RestController
  2. public class HelloController {
  3. ? ? @Value("${student.sname}")
  4. ? ? private String sname;
  5. ? ? @RequestMapping("/hello")
  6. ? ? public String hello(){
  7. ? ? ? ? return "hello"+sname;
  8. ? ? }
  9. }

配置文件:

  1. #配置student
  2. student.sname=zhai
  3. student.age=12
  4. student.maps.k1=1
  5. student.maps.k2=2
  6. student.list=a,b,c
  7. student.course.courseno=202007
  8. student.course.coursename=java

3、@PropertySource

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

  1. #配置student
  2. student.sname=zhai
  3. student.age=12
  4. student.maps.k1=1
  5. student.maps.k2=2
  6. student.list=a,b,c
  7. student.course.courseno=202007
  8. student.course.coursename=java

(2)实体类获取值

  1. @Data
  2. @Component
  3. @PropertySource(value = {"classpath:student.properties"})
  4. public class Student {
  5. ? ? private String sname;
  6. ? ? private int age;
  7. ? ? private Map<String,Object> maps;
  8. ? ? private List<Object> list;
  9. ? ? private Course course;
  10. }

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

4、@ImportResource和@Bean

(1)指定spring的配置文件

  1. @SpringBootApplication(scanBasePackages = "com")
  2. @ImportResource(locations = {"classpath:beans.xml"})
  3. public class Springboot02Application {
  4. ? ? public static void main(String[] args) {
  5. ? ? ? ? SpringApplication.run(Springboot02Application.class, args);
  6. ? ? }
  7. }

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

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

  1. @Configuration
  2. public class MyAppConfig {
  3. ? ? @Bean
  4. ? ? public HelloService helloService(){
  5. ? ? ? ? return new HellService();
  6. ? ? }
  7. }

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

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

总结:

(1)@ConfigurationProperties与@Value

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号