@Value와 SPEL을 사용하면 쉽게 Properties에 설정된 값을 injection해줄 수 있다.
그런데.. Properties라면.. default값이 필요하지 않을까?? 해당 값이 properties 파일에 들어 있지 않다면 말짱 꽝일테니.. 나는 @Value를 써서 설정되어 있기를 바랬는데 null이 나오면 정말 황당할 것이다.
이럴 때 다음과 같이 쓰면 가볍게 default값을 설정해 줄 수 있다.
- 해결 실마리 : @Value doesn't allow a default value
그런데.. Properties라면.. default값이 필요하지 않을까?? 해당 값이 properties 파일에 들어 있지 않다면 말짱 꽝일테니.. 나는 @Value를 써서 설정되어 있기를 바랬는데 null이 나오면 정말 황당할 것이다.
이럴 때 다음과 같이 쓰면 가볍게 default값을 설정해 줄 수 있다.
public SomeClass
{
@Value("#{someProps['id']?:'default'}")
private String id;
}
{
@Value("#{someProps['id']?:'default'}")
private String id;
}
<beans ...
<util:properties id="someProps" location="classpath:my.properties" />
</beans>
<util:properties id="someProps" location="classpath:my.properties" />
</beans>
# my.properties
id=myId
ip=myIP
id=myId
ip=myIP
- 해결 실마리 : @Value doesn't allow a default value
덧글