application.properties
이외에도 xxx.properties
의 값을 읽어오는 간단한 예제입니다.
src/main/resources/test.properties
src/.../ConfigUtil.java
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Slf4j
@Configuration // 빈 등록
@RequiredArgsConstructor
@PropertySource("classpath:test.properties") // classpath는 src/main/resource/ 입니다.
public class ConfigUtil {
private final Environment environment; // 빈 주입을 받습니다.
public String getProperty(String key){
return environment.getProperty(key);
}
}
src/.../TestController.java
import com.easyconv.easyconvserver.config.ConfigUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequiredArgsConstructor
@RestController
public class TestController {
private final ConfigUtil configUtil;
@GetMapping("/prop/{key}")
public ResponseEntity<?> getPropertyValue(@PathVariable String key){
String value = configUtil.getProperty(key);
return ResponseEntity.ok(value);
}
}