일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 젠킨스초기설정
- restapi
- Flutter
- springboot
- springboot세팅
- dynamicrouter
- DART
- flutter drawer
- googleoath2연결
- naverapi
- vite
- 플루터
- 네이버API
- oath2
- vite.config.js
- drawer 설정
- Geolocator
- googlerefreshtoken
- API
- refreshtoken값
- 카카오API
- 로그제거
- refreshtoken값받기
- Java
- jenkins설치
- kakaoAPI
- spring
- Spring-boot
- vite설정
- 스프링부트
- Today
- Total
사소한것부터 바꾸자
Properties 파일 활용하기 본문
SpringBoot FramWork를 사용한다면 properties파일을 다들 한번씩 들어 보셨을텐데
참 처음 공부 하는 입장에선 해당 파일에 값을 입력하고 입력한 값을 사용하면 은폐도되고
편하게 사용 할텐데 어떻게 값을 불러와서 사용하는지 몰라서 난감 합니다. (물론 고수님들은 잘 사용하고 있겠지요)
properties 파일에 저장해서 캡슐화해서 DB에서 불러 오지 않아도 나름 안전하게 저장된 값을 가져오니 꼭 사용하고
싶은데 어떻게 사용하면 좋을지 몰라서 답답한 저같은 초보를 위해 적어 봅니다.
1. 클래스를 하나 만들고 아래의 코드를 입력 합니다.
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLoader {
public static Properties fromResource(String resource) throws IOException {
try (InputStream input = classLoader().getResourceAsStream(resource)) {
if (input == null) {
throw new FileNotFoundException(resource);
}
Properties properties = new Properties();
properties.load(input);
return properties;
}
}
private static ClassLoader classLoader() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
return loader == null ? PropertiesLoader.class.getClassLoader() : loader;
}
}
그리고
사용할 부분에 가서
불러오면 되는데
지난 Naver Rest Api 활용해서 설명 해 드리겠습니다.
번역부분 소스를 살짝 정리를 했습니다.
조금 더 간결하고 보기 좋게 정리를 하였는데 바로 이부분 입니다.
// 영어번역(Naver Rest Api)
@Override
public String getEnglish(MysecretDto msd) {
String apiURL = "https://openapi.naver.com/v1/papago/n2mt";
String result = "";
try {
Properties properties = PropertiesLoader.fromResource("api.properties");
String clientId = properties.getProperty("clientId");
String clientSecret = properties.getProperty("clientSecret");
HttpPost post = new HttpPost(apiURL);
post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
post.addHeader("X-Naver-Client-Id", clientId);
post.addHeader("X-Naver-Client-Secret", clientSecret);
StringBuilder json = new StringBuilder();
json.append("source=ko&target=en&text=" + msd.getKorean());
post.setEntity(new StringEntity(json.toString(), "UTF-8"));
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);
result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
response.close();
httpClient.close();
} catch (Exception e) {
result = e.getMessage();
}
return result;
}
위부분 확인 해보시면 Properties properties = PropertiesLoader.fromResource("api.properties");
이부분이 있어요 여기서 properties파일과 연결을 해줍니다.
("properties 파일명") -> test.peroperties 이와 같이 .properties까지 전부 입력 하셔야 합니다.
그리고
String clientId = properties.getProperty("clientId");
이렇게 불러 오는데
getProperty("속성명") 되겠습니다.
그러면 properties 파일 확인 해 볼까요?
이렇게 resoures에 api.properties 파일이 있지요

그리고 api.properties파일에는

이렇게 입력 되어 있습니다.
속성명=adfadsf (*단 ;세미클론은 입력하지 않습니다.)
이렇게 아주 쉽게 properties 파일의 값을 불러와서 사용하는 방법을 알아 보게 되었습니다.
감사합니다.
마지막 저의 git 주소 입니다. 소스 마구 가져다 사용하세요 ㅋㅋ
https://github.com/Byube/NaverRestApi
Byube/NaverRestApi
Contribute to Byube/NaverRestApi development by creating an account on GitHub.
github.com