添加配配置信息
spring:
application:
name: dataExChangePlatform
devtools:
restart:
enabled: true
additional-paths: src/main/java
exclude: WEB-INF/**
freemarker:
cache: false
server:
port: 8000
repo:
name: Repository
type: mysql
access: Native
host: localhost
db: test
port: 3306
user: root
pass: toor@1234
依赖注入
package com.sugon.dataexchangeplatform.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Component // 注册bean
@ConfigurationProperties(prefix = "repo") // 从配置文件中获取值
@Validated // 数据校验
public class DataBaseMetaConfig {
private String name;
private String type;
private String access;
private String host;
private String db;
private String port;
private String user;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAccess() {
return access;
}
public void setAccess(String access) {
this.access = access;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getDb() {
return db;
}
public void setDb(String db) {
this.db = db;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "DataBaseMeta{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", access='" + access + '\'' +
", host='" + host + '\'' +
", db='" + db + '\'' +
", port='" + port + '\'' +
", user='" + user + '\'' +
", pass='" + pass + '\'' +
'}';
}
}
获取实例
package com.sugon.dataexchangeplatform;
import com.sugon.dataexchangeplatform.domain.DataBaseMetaConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DataExChangePlatformApplicationTests {
@Autowired // // 将DataBaseMetaConfig自动注入进来
DataBaseMetaConfig config;
@Test
void contextLoads() {
System.out.println(config);
}
}
标签:return,String,name,void,yml,port,application,public,springboot
From: https://www.cnblogs.com/guanchaoguo/p/17116922.html