SpringBoot对properties的使用

mac2024-03-12  34

在我们的日常开发中总是免不了一些配置文件,SpringBoot是让我们更好地利用spring框架,简化spring框架复杂的配置,让我们更加专注于业务!spring中的配置方式在SpringBoot同样支持! spring有属性注入,在SpringBoot同样支持!

属性注入

plication.properties文件中添加如下自定义配置:

dept.id=D00001 dept.name=物联网人工智能产品部 dept.address=广州

定义一个Dept.java,按照spring的方式可以通过@Value将值注入进对象属性中:

@Component public class Dept { @Value("${dept.id}") private String id; @Value("${dept.name}") private String name; @Value("${dept.address}") private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Dept [id=" + id + ", name=" + name + ", address=" + address + "]"; } }

测试类 DeptController.java :

@RestController public class DeptController { @Autowired private Dept dept; @GetMapping(value="/getDept") public String getDept() { return dept.toString(); } }

访问:http://localhost:8093/getDept 可以看到我们成功的访问到了数据,这是我们将数据存放到了application.properties中,在开发中application.properties文件中应该只是存放系统配置信息,我们自定义的配置信息原则上是不能跟系统配置信息放在一起的,在SpringBoot中同样支持自定义配置文件,如下:

在resources下新建dept.properties,并添加如下信息:

dept.id=D00001 dept.name=物联网人工智能产品部 dept.address=广州

定义 Dept.java

@Component @PropertySource("classpath:dept.properties") public class Dept { @Value("${dept.id}") private String id; @Value("${dept.name}") private String name; @Value("${dept.address}") private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Dept [id=" + id + ", name=" + name + ", address=" + address + "]"; } }

这样在系统系统启动之后就会加载dept.properties文件,但这不是SpringBoot特有的配置,之所以说不是SpringBoot特有的配置是因为添加 @PropertySource 之后导的包是spring中的context下的,如下:

import org.springframework.context.annotation.PropertySource;

类型安全的属性注入

那么在SpringBoot中是 如何使用的呢,这里不得不说下类型安全的属性注入,当配置属性非常多的时候,用@Value就会显得臃肿了,而且还容易出错,在视觉上也不美观!

在resources下新建dept.properties (文件名随便起,但是以 .properties结尾) 在dept.properties文件中自定义配置:

dept.id=D00001 dept.name=物联网人工智能产品部 dept.address=广州

定义Dept.java 实体类用于接受dept.properties注入进来的值,其中@Component @PropertySource(“classpath:dept.properties”) 是指向自定义配置文件路径;@ConfigurationProperties(prefix = “dept”) 是指向配置文件中属性的前缀。

@Component @PropertySource("classpath:dept.properties") @ConfigurationProperties(prefix = "dept") public class Dept { private String id; private String name; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Dept [id=" + id + ", name=" + name + ", address=" + address + "]"; } }

注:

@ConfigurationProperties是 springBoot的注解 @PropertySource 是 spring中context的注解 导入的包如下: import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource;

访问:http://localhost:8093/getDept 结果如下:

最新回复(0)