전통?적인 PropertyPlaceholderConfigurer 과 SpEL

전통적인? 방법과 SpEL을 사용해 properties파일 가져오는 방법

1. PropertyPlaceholderConfigurer 사용
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
xmlns:util="http://www.springframework.org/schema/util">
<context:annotation-config />
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/data.properties</value>
</list>
</property>
</bean>
<!-- Redis Source -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- Redis Source -->
</beans>

2. SpEL 사용
아래 2개로 깔끔하게 처리
- <util:properties id="dataProps" location="classpath:properties/data.properties">
- #{dataProps['redis.host']}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
xmlns:util="http://www.springframework.org/schema/util">
<context:annotation-config />
<util:properties id="dataProps"
location="classpath:properties/data.properties" />
<!-- Redis Source -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="#{dataProps['redis.host']}" />
<property name="port" value="#{dataProps['redis.port']}" />
<property name="password" value="#{dataProps['redis.password']}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- Redis Source -->
</beans>

댓글

이 블로그의 인기 게시물

WTP에서 Axis2를 이용한 웹 서비스 구현

Android, MediaPlayer IllegalStateException 상황