【spring boot】映射properties文件属性--到Java对象
2017-06-08 17:03
423 查看
描述
将*.properties中的内容映射到java对象中;主要步骤
添加 @Component 注解;使用 @PropertySource 注解指定配置文件位置;
使用 @ConfigurationProperties 注解,设置相关属性;
my.properties
author.name=ssslinppp author.age=128 author.student.name=studentName author.student.age=88
AuthorSettings.java
package com.sssppp; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "author") @PropertySource("classpath:my.properties") public class AuthorSettings { private String name; private Long age; private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getAge() { return age; } public void setAge(Long age) { this.age = age; } }
Student.java
package com.sssppp; public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
测试类
package com.sssppp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Ch623 { @Autowired private AuthorSettings authorSettings; @RequestMapping("/ch623") public AuthorSettings index() { return authorSettings; } }
测试结果
{"name":"ssslinppp","age":128,"student":{"name":"studentName","age":88}}
相关文章推荐
- 【spring boot】使用@Value映射properties文件属性
- SpringBoot属性文件配置(application.properties/yml)
- Springboot属性文件application.properties配置大全,版本:1.5.4
- spring boot-application.properties配置文件属性
- Spring Boot属性文件application.properties配置文档(全部)
- 【spring boot】配置文件 application.properties 属性解析
- springboot之读取properties属性文件
- SpringBoot 使用注解将配置文件自动映射到属性和实体类
- spring boot 注入properties属性文件的值 @ConfigurationProperties
- Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- spring加载properties属性文件到内存
- Java中如何获取spring中配置的properties属性文件内容
- 如何在spring中使用properties文件中属性
- mybatis3.0x升级整合spring问题(读取不到properties文件属性)
- spring中属性文件properties的读取
- Spring Boot 引入 properties 文件的笔记
- Spring中属性文件properties的读取与使用
- java spring中对properties属性文件加密及其解密
- Spring中属性文件properties的读取与使用