-
스프링 Properties 관련 정리SPRING 2020. 4. 15. 14:24
프로퍼티
- 다양한 방법으로 정의할 수 있는 설정 값
- Environment는 properties source 설정과 properties value를 가져오는 역할을 한다.
12345678910111213141516package com.example.demo;import org.springframework.context.annotation.PropertySource;@SpringBootApplication@PropertySource("classpath:/app.properties")public class Demo1Application {public static void main(String[] args) {SpringApplication.run(Demo1Application.class, args);}}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter아래와 같이 @PropertySource 어노테이션을 이용하여 app.properties 위치를 넣어주면, Environment 객체에 프로퍼티 값이 주입된다.
이떄 Root 경로는 src/main/resources가 잡힌다.
1. @Value 를 이용하면 아래와 같이 Property 값을 가져올 수 있다.
2. Envornment 객체의 getProperty 메서드를 이용하는 방법을 사용해도 된다.
1234567891011121314151617181920212223242526272829303132333435package com.example.demo;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class AppRunner implements ApplicationRunner {@AutowiredApplicationContext ctx;@AutowiredBookRepository bookRepository;@Value("${app.name}")String appName;@Overridepublic void run(ApplicationArguments args) throws Exception {// TODO Auto-generated method stubEnvironment environment = ctx.getEnvironment();System.out.println(appName);}}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter두 경우 모두 application.properties 에 선언한 app.name의 value 값이 찍힌다.
'SPRING' 카테고리의 다른 글
스프링 이벤트 프로그래밍 -ApplicationEventPublisher (0) 2020.04.19 스프링 MessageSource (0) 2020.04.15 스프링 프로파일 (Profile) (0) 2020.04.14 스프링 빈 스코프 (싱글톤, 프로토타입) (0) 2020.04.12 Component와 ComponentScan 정리 (0) 2020.04.10