Spring Framework
[Properties] Java 개발 시 사용하게 되는 Properties 사용법
헤르메스의날개
2021. 1. 17. 21:56
728x90
개발을 하다보면 Properties 를 많이 사용하게 됩니다.
DB 에 저장하기에는 무겁고, 그냥 하드코딩하기는 나중이 걱정스럽죠. 그리고, 생각보다 꽤 많은 부분에서 Properties는 사용되는 모습을 보게 됩니다.
그래서 하나하나 정리해보기로 했습니다.
1. Property Service ( 전자정부 프레임워크 )
참고 : www.egovframe.go.kr/wiki/doku.php?id=egovframework:rte:fdl:property
<bean name="propertyService" class="egovframework.rte.fdl.property.impl.EgovPropertyServiceImpl"
destroy-method="destroy">
<property name="properties">
<map>
<entry key="AAAA" value="1234"/>
</map>
</property>
</bean>
@Resource(name="propertyService")
protected EgovPropertyService propertyService ;
@Test
public void testPropertiesService() throws Exception {
assertEquals("1234",propertyService.getString("AAAA"));
}
2. Spring Boot ( @Value Anotation )
appication.yml ( application.propertis 를 사용해도 같은 내용입니다.)
spring:
profiles:
active: local # 디폴트 환경
# properties 파일 내용
spring.profiles.active=local
@RestController
@RequestMapping( value = { "api" } )
@PropertySource("classpath:appication.yml")
public class TestController {
private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
@Value("${spring.profiles.active}")
private String env;
public @ResponseBody
Map selectList( @RequestParam Map<String, Object> param, HttpServletRequest request ) throws Exception {
if(env.equals("local")) {
return "simpleRestService.selectTestList( param );
} else {
return "simpleRestService.selectList( param );
}
}
}
728x90