develog

[Spring] Spring 2.5, Properties #2 본문

Dev/Spring

[Spring] Spring 2.5, Properties #2

냐옴 2014. 11. 11. 10:57

TestMainProperties.java

import my.PropClient;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestMainProperties {


public static void main(String[] args) throws Exception {

System.out.println("START");

System.out.println("------------------------------------------------");


ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml");

PropClient propClient = (PropClient) context.getBean("propClient");

System.out.println("propClient.getValue() : " + propClient.get("name11"));

System.out.println("propClient.getValue() : " + propClient.get("name12"));


System.out.println("------------------------------------------------");

System.out.println("END");

}


}


PropClient.java

 package my;


import java.util.Properties;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;


@Component

public class PropClient {


@Autowired

private Properties myProps;


public String get(String key) {

return myProps.getProperty(key);

}


}


spring-config.xml

 <beans

default-autowire="no" 

        default-lazy-init="false" 

        default-dependency-check="none" 

    xmlns="http://www.springframework.org/schema/beans" 

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

   xmlns:context="http://www.springframework.org/schema/context" 

   xmlns:util="http://www.springframework.org/schema/util"

   xsi:schemaLocation="

   http://www.springframework.org/schema/beans 

   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 

   http://www.springframework.org/schema/context 

   http://www.springframework.org/schema/context/spring-context-2.5.xsd

   http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd

">


<context:component-scan base-package="my" />

<util:properties id="myProps" location="classpath:sample.properties" />

    

</beans>


sample.properties

 name11=val11

name12=val12


'Dev > Spring' 카테고리의 다른 글

[Spring] Spring3 AOP 설정, xml or java  (0) 2015.06.04
[Spring] static method injection  (0) 2014.11.11
[Spring] Spring 2.5, Properties  (0) 2014.11.10
[Spring] beans xml schema  (0) 2014.10.15
Spring 에서 HttpServletRequest 접근  (0) 2013.12.11
Comments