1. Foreword
1. The program code is written using idea2021.12 version, if you use other software, please check the configuration;
2. I forgot the specific content of this program. I only know that it is written using @Component@ConfigurationProperties@Override@SpringBootTest and other methods. The specific function is to create several .yml and .java files to realize configuration access in the .yml file The path (input information) is constructed in the .java file and output on its console;
3. The following is the structure and code segments that need to be built for this table and the results of the operation;
4. As mentioned in this blog post, I have released the code package to my resources, and if necessary, you can directly download and import it into your own id to see if it can run;
Second, structure
First of all, you need to build a package, so I won’t say much here, the registration I built is unit2-2;
In turn, create a new package in the src-main-java-com-example-unit22 file in the package, named vo
Next, create three Java class files Employee, Role, and Student in the vo software package. These three Java classes are used to construct methods.
Normally, there is a Unit22Application file in the unit22 file, and there is no need to add it (if not, add it)
Create a new application.yml file in the src-main-resources package, which is used to configure the access path
Finally, create the Unit22ApplicationTests java class in src-testjava-com-example-unit22 (you don’t need to build it if it already exists). This class is used to automatically inject the attribute values of the configuration file and output
The above is the structure of this code, and the following is the structure of the code. The red box is the code that needs to be written. The following will paste each code segment in turn.
Third, code segment
1.1.Employee.java class code
package com.example.unit22.vo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; //@Component indicates that the Role entity class is a component of SpringBoot @Component @ConfigurationProperties(prefix = "employee") //Create the Employee entity class public class Employee { //@Value injects the attribute value of the basic data type in the configuration file @Value("${employee.name}") private String name; @Value("${employee.birth}") private String birth; private List <String> likes; //Generate getter and public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirth() { return birth; } public void setBirth(String birth) { this.birth = birth; } public List<String> getLikes() { return likes; } public void setLikes(List<String> likes) { this.likes = likes; } //Generate toString method @Override public String toString() { return "Employee{" + "name='" + name + ''' + ", birth='" + birth + ''' + ", likes=" + likes + '}'; } }
1.2.Role.java class code
package com.example.unit22.vo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; //@Component indicates that the Role entity class is a component of SpringBoot @Component //@ConfigurationProperties injects property values of complex data types in configuration files, and setter methods must be created @ConfigurationProperties(prefix = "role") public class Role { //@Value injects the attribute value of the basic data type in the configuration file @Value("${role.name}") private String name; @Value("${role.description}") private String description; private List <Long> permissionIds; // Generate get and methods public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List<Long> getPermissionIds() { return permissionIds; } public void setPermissionIds(List<Long> permissionIds) { this.permissionIds = permissionIds; } //Generate toString method @Override public String toString() { return "Role{" + "name='" + name + ''' + ", description='" + description + ''' + ", permissionIds=" + permissionIds + '}'; } }
1.3.Student.java class code
package com.example.unit22.vo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; @Component @ConfigurationProperties(prefix = "student") public class Student { @Value("${student.name}") private String name; @Value("${student.sno}") private String sno; private List<String> families; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public List<String> getFamilies() { return families; } public void setFamilies(List<String> families) { this.families = families; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", sno=" + sno + ", families=" + families + '}'; } }
2. Unit22ApplicationJAVA class code
package com.example.unit22; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Unit22Application { public static void main(String[] args) { SpringApplication.run(Unit22Application.class, args); } }
3. application.yml file code
#Configure access path server: servlet: context-path: /unit22 # Configure the basic information of the administrator role role: name: admin description: administrator permissionIds: - 10 - 11 - 12 # Configure employee information employee: name: Li Si birth: 2000-10-10 likes: - look beautiful - whistle - chat - cross flower # Configure student information student: name: CSDN sno: 2104240001 families: - mary - Feibo - tom - Conan
4. Unit22ApplicationTests java class code
package com.example.unit22; import com.example.unit22.vo.Employee; import com.example.unit22.vo.Role; import com.example.unit22.vo.Student; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class Unit22ApplicationTests { //@Autowired automatically injects the attribute value of the configuration file @Autowired private Role role; @Autowired private Employee employee; @Autowired private Student student; @Test void contextLoads() { System.out.println(role); System.out.println(employee); System.out.println(student); } }