SpringBoot属性配置中获取值的方式是什么
这篇文章主要介绍“SpringBoot属性配置中获取值的方式是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot属性配置中获取值的方式是什么”文章能帮助大家解决问题。
SpringBoot 属性配置中获取值在配置文件中定义属性的值,然后再获取,在这里做一个总结,首先,在application.yml文件中定义端口和属性的值,当然,在.application.properties文件中也能定义,只是格式不同而已:
appliaction.yml:
server:port:8081cubSize:Bage:18
然后再定义一个controller,用@Value这个注解来获取到值:
packagecom.dist.tr.controller;importcom.dist.tr.entity.GrilProperties;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMappingpublicclassBeautifulGirlContrller{@Value("${cubSize}")privateStringcubSize;@Value("${age}")privateIntegerage;@RequestMapping(value="/gril",method=RequestMethod.GET)publicStringHelloGril(){returncubSize+age;}}
运行结果:
如果当属性很多之后,要写很多的@Value 的注解嘛???答案当然是No,有简便的写法:
application.yml 文件
server:port:8081gril:name:lisaheight:165首先,定义一个实体类去写属性
GrilProperties实体:
注意我们用到了这个注解:@ConfigurationProperties(prefix = “gril”)
packagecom.dist.tr.entity;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix="gril")publicclassGrilProperties{privateStringname;privateStringheight;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetHeight(){returnheight;}publicvoidsetHeight(Stringheight){this.height=height;}}
在controller 中的写法:
首先用注解@Autowired 注入这个实体,如果不在实体中加@Component这个注解,在idea中发现会有红线出现。
packagecom.dist.tr.controller;importcom.dist.tr.entity.GrilProperties;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMappingpublicclassBeautifulGirlContrller{@AutowiredprivateGrilPropertiesgrilProperties;@RequestMapping("/grilPerproties")publicStringgrilPerproties(){returngrilProperties.getName()+grilProperties.getHeight();}}
运行结果:
这样就不会需要去写太多的@Value注解了。
还有中形式,就是在配置文件中也可以有这种情况出现:
server:port:8081cubSize:Bage:18context:"cubSize:${cubSize},age:${age}"
这种情况证明获取的属性值呢?
在controller中编码:
packagecom.dist.tr.controller;importcom.dist.tr.entity.GrilProperties;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMappingpublicclassBeautifulGirlContrller{@Value("${context}")privateStringcontext;@RequestMapping("/grilSize")publicStringgirlcubSize(){returncontext;}}
运行结果:
测试和生产区分当在项目开发的时候,如果区分测试和生产环境,那么就得区分开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将本类中的所有属性和配置文件的相关配置进行绑定publicclassStudent{//prefix:配置文件中哪一个名称下面的属性进行一一映射privateStringsname;privateintage;privateMap<String,Object>maps;privateList<Object>list;privateCoursecourse;}@DatapublicclassCourse{privateStringcourseno;privateStringcoursename;}
(2)创建yaml配置文件:
student:sname:zhaiage:12maps:{k1:12,k2:13}list:-zhai-zhangcourse:courseno:202007coursename: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)测试类:
//可以在测试期间很方便地类似编码一样进行自动注入等容器的功能@SpringBootTestclassSpringboot03ApplicationTests{@AutowiredStudentstudent;@TestvoidcontextLoads(){System.out.println(student);}}
(5)需要导入的依赖:配置文件处理器,配置文件进行绑定会有提示
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.2.1.RELEASE</version></dependency>
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@ComponentpublicclassStudent{@Value("${student.sname}")privateStringsname;@Value("#{2*9}")privateintage;privateMap<String,Object>maps;privateList<Object>list;privateCoursecourse;}
(3)@ConfigurationProperties(prefix = "")方式与@Value方式的比较
@ConfigurationProperties(prefix = "")方式支持批量注入配置文件的属性,@Value方式需要一个个指定
@ConfigurationProperties(prefix = "")方式支持松散绑定,@Value方式不支持,
yml中写的last-name,这个和lastName是一样的,后面跟着的字母默认是大写的。这就是松散绑定
@Value方式支持JSR303校验
@Data@Component@ValidatedpublicclassStudent{@NonNullprivateStringsname;privateintage;privateMap<String,Object>maps;privateList<Object>list;privateCoursecourse;}
@Value方式支持SpEl
如果我们只是在某一个业务逻辑中需要获取配置文件的某一项值,可以使用@Value,如果是一个javaBean来和配置文件进行映射,则要使用@ConfigurationProperties(prefix = "")方式
@RestControllerpublicclassHelloController{@Value("${student.sname}")privateStringsname;@RequestMapping("/hello")publicStringhello(){return"hello"+sname;}}
配置文件:
#配置studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java3、@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"})publicclassStudent{privateStringsname;privateintage;privateMap<String,Object>maps;privateList<Object>list;privateCoursecourse;}
@PropertySource是从指定路径下获取数据,默认是从application.properties下获取数据
4、@ImportResource和@Bean(1)指定spring的配置文件
@SpringBootApplication(scanBasePackages="com")@ImportResource(locations={"classpath:beans.xml"})publicclassSpringboot02Application{publicstaticvoidmain(String[]args){SpringApplication.run(Springboot02Application.class,args);}}
(2)书写spring的配置文件:beans.xml
(3)书写如下配置,可以省略配置文件的书写,用注解来代替
@ConfigurationpublicclassMyAppConfig{@BeanpublicHelloServicehelloService(){returnnewHellService();}}
@Configuration说明这是一个配置类,就是在替代之前的配置文件
@Bean标记在方法上,该方式将方法的返回值添加到容器中,容器中组件的ID默认是方法名
关于“SpringBoot属性配置中获取值的方式是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。