티스토리 뷰
728x90
출처
- Spring Properties : Albothyl 정리노트.
- [Spring Boot] Properties Configuration : 아키텍처와 함께 by gregorio
- [Maven] Maven Profile을 이용한 Springboot 배포서버 별 환경 구성하기 :: 프로그래민👨💻
Maven pom.xml 설정
Maven 배포시 프로파일 선택 옵션 : mvn clean package -P prod
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/bluexmas-${profile}</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<!-- 생략 -->
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile>dev</profile>
</properties>
</profile>
<profile>
<id>local</id>
<properties>
<profile>local</profile>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile>prod</profile>
</properties>
</profile>
</profiles>
src\main\resources\bluexmas-dev\config\config.properties
username=test
password=test!!
@Configuration 클래스 (PropertyConfig.java)
@Bean(name="config") : Bean 객체 config 이름으로 스프링 컨테이너에 로딩
package com.bluexmas.config;
import java.io.IOException;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
public class PropertyConfig {
@Bean(name="config")
public static PropertiesFactoryBean config() throws IOException {
PropertiesFactoryBean pspc = new PropertiesFactoryBean();
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath:/config/config.properties");
pspc.setLocations(resources);
return pspc;
}
}
Config 값 참조
@Resource(name="config") : config 이름의 Properties 객체 참조
@Value("#{config['username']}") : config 이름의 Properties 객체에서 username 값 참조
package com.bluexmas.controller;
import java.util.Properties;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SampleController {
@Resource(name="config")
private Properties config;
@Value("#{config['username']}")
private String username;
@ResponseBody
@RequestMapping("/sample")
public String sample() {
System.out.println("username.1 = " + config.get("username"));
System.out.println("username.2 = " + username);
String data = "@ResponseBody 어노테이션을 통해 반환";
return data;
}
}
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- sas2009
- ubuntu
- Linux
- 지스타2007
- Xcode
- ble
- ffmpeg
- Delphi
- 튜닝쇼 2008
- 서울오토살롱
- 송주경
- JavaScript
- Java
- Spring
- NDK
- 일본여행
- oracle
- flex
- Spring MVC
- KOBA
- Mac
- SAS
- 동경
- 전예희
- 레이싱모델 익스트림 포토 페스티벌
- MySQL
- koba2010
- android
- BPI-M4
- Delphi Tip
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함