Spring Boot讀取yml或者properties配置信息
方法一:@Value獲取基本信息,適用于少量信息
package com.geekmice.springbootselfexercise;
import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {
@Value("${server.port}")
private String port;
@Test
void contextLoads() {
log.info("端口號(hào):【{}】",port);
}
}
方法二:通過注解@ConfigurationProperties(prefix = “spring.datasource”)
編寫配置類
package com.geekmice.springbootselfexercise.config;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.config
* @Author: pingmingbo
* @CreateTime: 2023-08-05 23:12
* @Description: TODO
* @Version: 1.0
*/
@ConfigurationProperties(prefix = "spring.datasource")
@Component
@Data
public class DataSourceProperties {
private String username;
private String password;
private String url;
private String driverClassName;
}
開始使用
package com.geekmice.springbootselfexercise;
import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {
@Autowired
private DataSourceProperties dataSourceProperties;
@Test
void contextLoads() {
String username = dataSourceProperties.getUsername();
String password = dataSourceProperties.getPassword();
String url = dataSourceProperties.getUrl();
String driverClassName = dataSourceProperties.getDriverClassName();
log.info("用戶名:【{}】",username);
log.info("密碼:【{}】",password);
log.info("地址URL:【{}】",url);
log.info("驅(qū)動(dòng)類:【{}】",driverClassName);
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-629606.html
方法三:通過api Environment
package com.geekmice.springbootselfexercise;
import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {
@Autowired
private Environment environment;
@Test
public void t1(){
String username = environment.getProperty("spring.datasource.username");
String password = environment.getProperty("spring.datasource.password");
String url = environment.getProperty("spring.datasource.url");
String driverClassName = environment.getProperty("spring.datasource.driver-class-name");
log.info("用戶名:【{}】",username);
log.info("密碼:【{}】",password);
log.info("地址URL:【{}】",url);
log.info("驅(qū)動(dòng)類:【{}】",driverClassName);
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-629606.html
到了這里,關(guān)于Spring Boot讀取yml或者properties配置信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!