In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what is the way to get the value in the SpringBoot attribute configuration". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "what is the way to get the value in the SpringBoot attribute configuration" can help you solve the problem.
Get the value in the SpringBoot property configuration
Define the value of the property in the configuration file, and then get it, and make a summary here. First, define the port and the value of the property in the application.yml file. Of course, it can also be defined in the .application.properties file, but in a different format:
Appliaction.yml:
Server: port: 8081cubSize: Bage: 18
Then define a controller and use the @ Value annotation to get the 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;}}
Running result:
If you have a lot of attributes, do you have to write a lot of @ Value annotations? The answer, of course, is No. There are simple ways to write it:
Application.yml file
Server: port: 8081gril: name: lisa height: 165. first, define an entity class to write attributes.
GrilProperties entity:
Notice that we use this annotation: @ 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;}}
How to write it in controller:
First inject the entity with the annotation @ Autowired. If you don't add the @ Component annotation to the entity, you will find a red line in 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 ();}}
Running result:
So you don't have to write too many @ Value annotations.
There is also a medium form, which can also occur in the configuration file:
Server: port: 8081cubSize: Bage: 18context: "cubSize:$ {cubSize}, age:$ {age}"
What about the value of the attribute obtained by this situation?
Encode in 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;}}
Running result:
Distinction between testing and production
When developing a project, if you distinguish between test and production environments, you have to distinguish between application.yml files:
Create a new application-dev.yml file and an application-prod.yml file:
Then when using testing or production, the application.yml file says:
Spring: profiles: active: prod
Decide whether to use test environment or production environment.
SpringBoot get values and profile
@ ConfigurationProperties, @ Value, @ PropertySource, @ ImportResource and @ Bean
1. @ ConfigurationProperties (prefix = "student") mode
(1) define two entity classes, of which the attributes of the student entity class include the Course class:
@ Data@Component@ConfigurationProperties (prefix = "student") / / tells springboot to bind all the properties in this class and the relevant configuration of the configuration file public class Student {/ / prefix: which of the following properties in the configuration file to map private String sname; private int age; private Map maps; private List list; private Course course;} @ Datapublic class Course {private String courseno; private String coursename;} one by one
(2) create a yaml configuration file:
Student: sname: zhai age: 12 maps: {K1: 12 K2: 13} list:-zhai-zhang course: courseno: 202007 coursename: javaweb
(3) create a properties file:
# configure studentstudent.age=12student.sname=zhaistudent.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java
(4) Test class:
/ / the functions of containers such as @ SpringBootTestclass Springboot03ApplicationTests {@ Autowired Student student; @ Test void contextLoads () {System.out.println (student);}} can be easily injected just like coding during testing.
(5) dependencies that need to be imported: configure the file processor, and there will be a prompt for binding the configuration file.
Org.springframework.boot spring-boot-configuration-processor 2.2.1.RELEASE
2. @ Value mode
(1) write configuration documents
# configure studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java
(2) get the value:
@ Data@Componentpublic class Student {@ Value ("${student.sname}") private String sname; @ Value ("# {2x9}") private int age; private Map maps; private List list; private Course course;}
(3) comparison between @ ConfigurationProperties (prefix = "") mode and @ Value mode
The @ ConfigurationProperties (prefix = "") method supports the attributes of the batch injection configuration file, and the @ Value method needs to be specified one by one.
Loose binding is supported in @ ConfigurationProperties (prefix = "") mode, but not in @ Value mode.
Last-name written in yml, which is the same as lastName, followed by letters in uppercase by default. This is loose binding.
@ Value mode supports JSR303 check
@ Data@Component@Validatedpublic class Student {@ NonNull private String sname; private int age; private Map maps; private List list; private Course course;}
@ Value mode supports SpEl
If we only need to get a value of the configuration file in some business logic, we can use @ Value, and if it is a javaBean to map to the configuration file, we need to use the @ ConfigurationProperties (prefix = "") method.
@ RestControllerpublic class HelloController {@ Value ("${student.sname}") private String sname; @ RequestMapping ("/ hello") public String hello () {return "hello" + sname;}}
Configuration file:
# configure studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java3, @ PropertySource
(1) configuration file (student.properties)
# configure studentstudent.sname=zhaistudent.age=12student.maps.k1=1student.maps.k2=2student.list=a,b,cstudent.course.courseno=202007student.course.coursename=java
(2) the entity class obtains the value.
@ Data@Component@PropertySource (value = {"classpath:student.properties"}) public class Student {private String sname; private int age; private Map maps; private List list; private Course course;}
@ PropertySource acquires data from the specified path. The default is to obtain data from application.properties.
4, @ ImportResource and @ Bean
(1) specify the configuration file for spring
SpringBootApplication (scanBasePackages = "com") @ ImportResource (locations = {"classpath:beans.xml"}) public class Springboot02Application {public static void main (String [] args) {SpringApplication.run (Springboot02Application.class, args);}}
(2) write the configuration file of spring: beans.xml
(3) write the following configuration. You can omit the writing of the configuration file and replace it with comments.
@ Configurationpublic class MyAppConfig {@ Bean public HelloService helloService () {return new HellService ();}}
@ Configuration indicates that this is a configuration class that replaces the previous configuration file
The @ Bean tag is on the method, which adds the return value of the method to the container. The ID of the component in the container is the method name by default.
This is the end of the content about "how to get the value in the SpringBoot property configuration". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.