[SpringBoot] application.properties μ΄μ체μ λ³ μ€μ
Spring Boot μ ν리μΌμ΄μ μ κ°λ°ν λ, μ ν리μΌμ΄μ μ νκ²½μ€μ νμΌμΈ application.propertiesλ application.ymlμ μ€μν μν μ νλ€.
νμ§λ§ κ°λ° νκ²½μ΄λ μ΄μ νκ²½μ λ°λΌ λ€λ₯΄κ² λμν΄μΌ νλ μ€μ λ€μ΄ μ‘΄μ¬νλ€.
μλ₯Ό λ€μ΄, νμΌ μ μ₯ κ²½λ‘ κ°μ κ²½μ°μλ μ΄μ체μ μ λ°λΌ λ¬λΌμ§ μ μλ€.
μ΄λ¬ν κ²½μ°μ λμΌν μ½λλ² μ΄μ€μμ μ΄μ체μ μ λ§κ² μ€μ μ λΆλ¦¬νκ³ κ΄λ¦¬νλ λ°©λ²μ λν΄ μμλ³΄λ €κ³ νλ€!
1. application.properties νμΌμμ κΈ°λ³Έ μ€μ μ μ
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
λ¨Όμ , application.properties νμΌμ 곡ν΅μ μΈ μ€μ μ μ μ΄λλ€.
2. μ΄μ체μ λ³ νλ‘νμΌ νμΌ μμ±
μ΄μ체μ λ§λ€ λ€λ₯Έ μ€μ μ μ μ©νλ €λ©΄ Springμ νλ‘νμΌ(profile) κΈ°λ₯μ νμ©ν μ μλ€.
application-windows.properties, application-mac.propertiesμ κ°μ νμΌμ λ§λ€μ΄ μ΄μ체μ λ³λ‘ μ€μ μ λΆλ¦¬ν μ μλ€.
Windowsμ© μ€μ (application-windows.properties)
spring.servlet.multipart.location=C:/Apache24/htdocs/
Macμ© μ€μ (application-mac.properties)
spring.servlet.multipart.location=/Users/username/Sup/
3. μ΄μ체μ λ³ νλ‘νμΌ μλ μ€μ
package com.example.studyproject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.studyproject")
public class StudyProjectApplication {
public static void main(String[] args) {
// μ΄μ체μ κ°μ§
String os = System.getProperty("os.name").toLowerCase();
System.out.println(os);
if (os.contains("win")) {
System.setProperty("spring.profiles.active", "windows");
} else if (os.contains("mac")) {
System.setProperty("spring.profiles.active", "mac");
} else {
System.setProperty("spring.profiles.active", "default"); // κΈ°ν μ΄μ체μ
}
SpringApplication.run(StudyProjectApplication.class, args);
}
}
μ΄ μ½λμμλ μ΄μ체μ λ₯Ό κ°μ§ν ν, ν΄λΉ μ΄μ체μ μ λ§λ νλ‘νμΌμ μλμΌλ‘ μ€μ νλ€.
osλ₯Ό μΆλ ₯ν΄λ΄€λλ μμ κ°μ΄ λ΄λ€
νμΌμ μ μ₯ν΄λ³΄λ application-windows.properties κ²½λ‘μ λ°λΌ μ μ μ₯λλ κ±Έ λ³Ό μ μμλ€~!