java/Spring Boot

application.properties 의 속성 get

내가 만드는게 길이 된다 2023. 11. 23. 13:55
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class AjaxController {
	private static final Logger LOGGER = LoggerFactory.getLogger(AjaxController.class);
	
	
	@Autowired
	private Environment env;
	
	@Value("${spring.profiles.active}")
	private String activeProfile;	
	
	@RequestMapping(value="/test.do")
	public String test() {
		System.out.println("test.do ===============================start");
		
		System.out.println("activeProfile================>"+activeProfile);
		
		String sKey = "server.port";
		String sVal = env.getProperty(sKey);
		
		System.out.println(sKey + " : " + sVal);
		
		return "helloJson";
	}
}

테스트 >>
http://localhost:8090/test.do

test.do ===============================start
activeProfile================>default
server.port : 8090


/*************************************************************
**.application.properties 파일 다른 환경으로 여러개 만들기
 *************************************************************
1.파일복사해서 생성
  application.properties ==> application-linux.properties
2.tomcat server vm 파라미터 추가
  2.1.tomcat server 더블클릭
  2.2.Open launch configuration 클릭
  2.3.Argument 탭클릭
  2.4.VM arguments 에 아래 추가
      -Dspring.profiles.active="linux"
      (원리 : -D키값=키value)

Profile