In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how the SpringBoot configuration file to the entity injection value related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this SpringBoot configuration file to the entity injection value article will have a harvest, let's take a look.
Configuration file injects values to entities
The default global profile for SpringBoot has two application.properties and application.yml
Properties usage # such as modifying port number direct assignment server.port=8888yaml usage # such as modifying port number server: port: 8888
The assignment in YML is in the form of key-value pairs, and the hierarchical relationship is expressed by indentation, and the indentation of the same level must be consistent.
Case-sensitive, loose writing is supported, and a space must be followed by a colon
Inject attributes / / entity class @ Component / / into the spring container @ ConfigurationProperties (prefix = "student") / / identify the configuration file public class Student {private String name; private int age; private boolean sex; private Date birthday; private Map location; private String [] hobbies; private List skills; private Pet pet; / / through the configuration file. Set/get/toString} # yml configuration file student: name: zs age: 23 sex: true birthday: 2020-10-1 location: {province: Hubei, city: Wuhan, zone: Hongshan} hobbies: [football, basketball] skills: [programming, finance] pet: nickName: wc strain: hsq
Here, pay attention to the writing of different data types, simple data direct assignment
# two words for map type and object type # 1. Inline location: {province: Hubei, city: Wuhan, zone: Hongshan} pet: {nickName: wc, strain: hsq} # 2. The branch writes location: province: Hubei city: Wuhan zone: Hongshan pet: nickName: wc strain: hsq# array, list, set# 1, inline writing, in which square brackets can also omit hobbies: [football, basketball] skills: [programming] Finance] # 2. The branch writes hobbies:-football-basketball skills:-programming-finance
Test output to SpringBoot test file
@ Autowired Student student; @ Test void contextLoads () {System.out.println (student);}
Result
Student {name='zs', age=23, sex=true, birthday=Thu Oct 01 00:00:00 CST 2020, location= {province= Hubei, city= Wuhan, zone= Hongshan}, hobbies= [football, basketball], skills= [programming, finance], pet=Pet {nickName='wc', strain='hsq'}}
Note in the above yml # what is the difference between double quotes if you add single quotes to a string location: {province: "Hubei", city: 'Wuhan', zone: Hongshan} # if the quotation marks are just strings, there is no difference location: {province: "Hu\ nbei", city:'Wu\ nHan', zone: Hong\ nShan} # at this point, the escaped characters in the double quotes will be recognized and will be wrapped Single quotation marks and no quotation marks will output as is or you can use properties to inject student.name=lsstudent.age=24...
The values in the properties file and the yml file can refer to each other. The two profiles can complement each other, and the priority of properties files is higher than yml.
In addition, if the value to be injected is not in these two default configuration files, you need to use the @ PropertySource annotation. Unfortunately, this annotation only supports properties files, not yml files.
@ PropertySource (value = {"classpath:conf.properties"}) public class Student {... Profile value injection 1.1 reads the configuration from the global configuration file to the entity class (@ ConfigurationProperties)
Configuration file (application.yml) (it can also be written in application.properties, just change the format)
Person: lastName: Mary age: 16 birthDate: 2004-01-01 maps: {one:1,two:2,three:3} lists:-1-2-3 pet: name: wangcai age: 3
Entity class (javaBean):
/ * Map the value of each property configured in the configuration file to this component * * @ Component: * only if this component is a component in the container can the @ ConfigurationProperties function provided by the container be enabled. * * @ ConfigurationProperties: * tell SpringBoot to bind all properties in this class and related configurations in the configuration file (the default configuration files are application.properties and * application.yml) * * prefix = "person": * which of the following attributes in the configuration file is mapped one by one * / @ Component@ConfigurationProperties (prefix = "person") public class Person {private String lastName; private Integer age; private Date birthDate; private Map maps; private List lists; private Pet pet; / / omit the get,set method @ Override public String toString () {String mapValue = "" If (this.mapsmaps null) {for (String key:this.maps.keySet ()) {Object value = this.maps.get (key); mapValue + = key+ "\ t" + value+ "\ t" }} return "lastName:" + this.lastName+ "\ nage:" + this.age+ "\ nbirthDate:" + this.birthDate+ "\ nmaps:" + mapValue+ "\ nlists:" + this.lists+ this.pet;}} public class Pet {private String name; private Integer age / / omit the get,set method @ Override public String toString () {return "\ nPet:\ n\ tPet name:" + this.name+ "\ n\ tPet age:" + this.age+ "\ n";}}
We can import the configuration file processor, and we will be prompted to write the configuration later (pom.xml).
Org.springframework.boot spring-boot-configuration-processor true
test
@ SpringBootTestclass DemoApplicationTests {@ Autowired Person person; @ Test void contextLoads () {System.out.println (person.toString ());}} 1.2 Global profile injection values (@ Value)
@ Value is actually an annotation in Spring, and its function is described in the xml file as follows:
Where property represents the attributes in the Person class, name is used to specify specific properties, and value is used to specify values.
There are three main values of value:
Literal quantity, that is, the number. ), string (abc), Boolean variable (true, false) and other constants
${key} to get values from environment variables and configuration files
# {SpEL}, the expression language of Spring, can perform some calculations and call some functions
@ Componentpublic class Person {@ Value ("${person.last-name}") / / @ Value ("# {'Lily'.toUpperCase ()}") private String lastName; @ Value ("# {11q2}") private Integer age; private Date birthDate; private Map maps; private List lists; private Pet pet; / / omit get,set and toString} 1.3 read and configure entity classes (@ PropertySource+@ConfigurationProperties) from the specified file
Create a person.properties under the resources directory
Person.lastName = Lilyperson.age = 20person.birthDate = 2000/01/01person.maps.one = 1person.maps.two = 2person.lists = a person. Pet.name = wangcaiperson.pet.age = 3
Change the comments in the Person class
Component@PropertySource (value = {"classpath:person.properties"}) @ ConfigurationProperties (prefix = "person") public class Person {/ /. Omit attribute, get,set,toString}
Note that PropertySource only supports properties files, not yml file reads.
1.4 read and inject values from the specified file (@ PropertySource+@Value)
Create a profile
Change comment
Component@PropertySource (value = {"classpath:person.properties"}) public class Person {@ Value ("${person.last-name}") private String lastName; / /. Omitting attributes, get,set,toString} is the end of the article on "how to inject values into an entity in a SpringBoot configuration file". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to configure SpringBoot files to inject values into entities". If you still want to learn more knowledge, you are welcome to follow the industry information channel.
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.