아키텍처와 함께

블로그 이미지
by gregorio
  • Total hit
  • Today hit
  • Yesterday hit

Spring Boot에서 Properties를 구성하는 방법이다.


먼저 Spring MVC에서는 XML 파일을 이용하여 다양하게 Property를 bean으로 정의할 수 있다.


■ properties-context.xml


 

<bean id="app" class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<property name="location" value="classpath:properties/app.properties" />

</bean>


<bean id="system" class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<property name="location" value="classpath:properties/system.properties" />

</bean>

<!-- system.properties, common.properties의 모든 항목을 Load  -->

<!-- 응용에서는 Property의 설정값을 가지고 오기 위해 PropertiesUtil을 사용함 -->

<bean id="PropertiesUtil" class="batch.web.util.PropertiesUtil">

  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />

  <property name="locations">

  <list>

  <value>classpath:properties/system.properties</value>

  <value>classpath:properties/app.properties</value> 

  </list>

  </property>

  </bean>


여기서 PropertiesUtil bean은 Context Load 시 app.properties와 system.properties를 모두 읽어 소스에서 모든 properties를 읽을 수 있도록 구성되어 있다.


 Spring Boot에서 Properties를 bean으로 생성하기 위해서는 PropertySourcesPlaceholderConfigurer를 이용하여 bean으로 설정한다.



■PropertyConfig.java


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="app")

public static PropertiesFactoryBean app() throws IOException {

PropertiesFactoryBean pspc = new PropertiesFactoryBean();

Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:properties/app.properties");

pspc.setLocations(resources);

return pspc;

}

@Bean(name="system")

public static PropertiesFactoryBean system() throws IOException {

PropertiesFactoryBean pspc = new PropertiesFactoryBean();

Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:properties/system.properties");

pspc.setLocations(resources);

return pspc;

}

}


PropertiesFactoryBean 를 bean으로 설정 시 location을 classpath의 properties/app.properties와 system.properties를 사용하여 각각을 app bean과 system bean으로 설정하였다.


app bean과 system bean을 사용하기 위해서는 bean이 동일하게 PropertiesFactoryBean를 bean으로 등록하기 때문에 어떤 bean을 사용할지 정의하여야 한다.


■ 사용 예


  @Autowired

@Qualifier("system")

private PropertiesFactoryBean system;



LOGGER.debug("Message Test :: {}::{}", msg,system.getObject().get("jndi.datasource.name"));



PropertiesFactoryBean으로 정의된 system bean을 사용하기 위해 Qualifier Annotation을 이용하여 system bean을 정의하였다.


이렇게 하지 않는 경우 Spring boot가 실행시 다음과 같은 오류 메세지가 발생한다.


Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed 






위에서 정의한 PropertiesUtil을 bean으로 정의하기 위해 다음과 같이 개발이 필요하다.


■PropertiesUtil.java


import java.util.HashMap;

import java.util.Map;

import java.util.Properties;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import org.springframework.core.io.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.stereotype.Component;



@Component

public class PropertiesUtil extends PropertyPlaceholderConfigurer implements InitializingBean{

/**LOGGER SET **/

private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);


private static Map<String, String> propertiesMap;



   @Override

    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {

        super.processProperties(beanFactory, props);

//         String[] activeProfiles = environment.getActiveProfiles();

//

//         if (LOGGER.isDebugEnabled()) {

//         for (String activeProfile : activeProfiles) {

//         LOGGER.debug("Active Profile :: {}", activeProfile);

//         }

//         }

        propertiesMap = new HashMap<String, String>();

        for (Object key : props.keySet()) {

            String keyStr = String.valueOf(key);

            String valueStr = resolvePlaceholder(keyStr, props, SYSTEM_PROPERTIES_MODE_OVERRIDE);

            propertiesMap.put(keyStr, valueStr);

            if (LOGGER.isDebugEnabled()) {

            LOGGER.debug("Load Properties :" +  keyStr + ":" + valueStr);

            }

        }

    }


    /**

     * This method return value with the name from properties map

     * @param name propertiy name

     * @return

     */

    public static String getString(String name)  {

        return propertiesMap.get(name) != null ? propertiesMap.get(name).toString() : "";

    }


    public static int getInt(String name) {

    return propertiesMap.get(name) != null ? Integer.parseInt(String.valueOf(propertiesMap.get(name))) : 0;

    }


    public static long getLong(String name) {

    return propertiesMap.get(name) != null ? Long.parseLong(String.valueOf(propertiesMap.get(name))) : 0L;

    }


@Override

public void afterPropertiesSet() throws Exception {

Resource[] locations  = new PathMatchingResourcePatternResolver().getResources("classpath:properties/*.properties");

LOGGER.debug("Locations :: {}", locations.toString());

super.setLocations(locations);

}

}



소스에서는 PropertyPlaceholderConfigurer의 processProperties 메소드를 재정의하였으며, 본 메소드에서는 Properties를 파라미터로 받아 해당 Properties를 Load한다.



InitializingBean의 afterPropertiesSet  메소드를 재정할 때 PropertyPlaceholderConfigurer의 location을 읽어들일 Properties 파일을 정의하여 사용한다.




'Spring Framrwork' 카테고리의 다른 글

[Spring Boot] JsonView 설정  (0) 2018.08.14
[Spring Boot] Exception Resolver  (0) 2018.08.14
[Spring Boot] Transaction AOP 설정  (0) 2018.08.13
[Spring Boot] Mybatis 적용  (0) 2018.08.13
[Spring Boot] Intercepor 적용  (0) 2018.08.13
AND

ARTICLE CATEGORY

분류 전체보기 (56)
Spring Framrwork (33)
Linux (1)
APM (1)
Java (8)
python (0)
ant (1)
chart (1)
OS (1)
tomcat (1)
apache (1)
database (0)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/05   »
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

ARCHIVE

LINK