記錄

Spring) Properties Class의 활용 본문

Web/Spring framework

Spring) Properties Class의 활용

surhommejk 2018. 4. 28. 14:22


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
IOC 컨테이너 (Spring 전용 메모리 공간) 안에 생성될 객체를 만들고 조립(의존)관계 설정 하는 파일
    BookClient bookclient = new BookClient();
        
        //Collection Framework : List , Set , Map , Properties(String,String)
        Properties prop = new Properties();
        prop.setProperty("server", "192.168.0.26");
        prop.setProperty("connectiontimeout", "20000");
        
        bookclient.setConfig(prop);
-->
    <bean id="bookclient" class="DI_10_Spring.BookClient">
        <property name="config">
            <!-- Properties 타입을 가진 객체의 주소 주입 -->
            <props>
                <prop key="server">192.168.0.26</prop>
                <prop key="connectiontimeout">20000</prop>
            </props>
        </property>
    </bean>
</beans>


package DI_10_Spring;

import java.util.Properties;

public class BookClient {
private Properties config;

public void setConfig(Properties config) {
        this.config = config;
}
//일반함수 (출력)
public void connect() {
     String server = config.getProperty("server");
     String timeout = config.getProperty("connectiontimeout");
    
     System.out.println("server : " + server);
     System.out.println("timeout :" + timeout);
}
}


public class Program {

    public static void main(String[] args) {
        /*
        BookClient bookclient = new BookClient();
        
        //Collection Framework : List , Set , Map , Properties(String,String)
        Properties prop = new Properties();
        prop.setProperty("server", "192.168.0.26");
        prop.setProperty("connectiontimeout", "20000");
        
        bookclient.setConfig(prop);
        bookclient.connect();
        */
        ApplicationContext context =
                new GenericXmlApplicationContext("classpath:DI_10_Spring/DI_10.xml");
        BookClient bookclient = context.getBean("bookclient",BookClient.class);
        bookclient.connect();
    }
}


java.util

Class Properties


public class Properties

extends Hashtable<Object,Object>


The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

A property list can contain another property list as its "defaults"; this second property list is searched if the property key is not found in the original property list.


Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key.


Comments