greeting:
message: Welcome to the User Service
첫 번째 방법. Env객체 사용하기
@RestController
@RequestMapping("/")
public class UserController {
private Environment env;
@Autowired
public UserController(Environment env) {
this.env = env;
}
@GetMapping("/welcome")
public String welcome(){
return env.getProperty("greeting.message");
}
}
두 번째 방법. @Value
사용하기
우선 클래스를 하나 만들어주고, @Component
어노테이션을 추가한다.
package com.example.userservice.vo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class Greeting {
@Value("${greeting.message}")
private String message;
}
//위의 컨트롤러에 추가
@GetMapping("/welcome-annotation")
public String welcomeAnnotation(){
return greeting.getMessage();
}