https://www.baeldung.com/properties-with-spring

Properties with Spring and Spring Boot | Baeldung

Tutorial for how to work with properties files and property values in Spring.

www.baeldung.com


 

원래는 *.properties 파일을 사용했었는데, 

우연히 *.properties에 관한 글을 보고 ( http://kwon37xi.egloos.com/4665590 ) 몇 가지 공감되는 부분들이 있어서

*.properties를 버리고 *.xml 을 사용하기로 마음 먹었다.

뭐, 아직 나는 초보수준이라서.. 자세한건 모르겠지만..

*.properties 파일의 한글 문제, 편집기 문제 등은.. *.properties파일을 사용하면서 불편하다고 느끼고 있었으니까..!

어쨌든, 그리하여 *.xml을 사용하기로 결정..! 그리고 또 까먹기전에 잘 정리해 둬야지...


1. 우선 *.xml 파일을 생성했다. 문법은 간단하다

 

1.<?xml version="1.0" encoding="UTF-8"?>
2.<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd" >
3.<properties>
4. 
5.<comment>설명</comment>
6.<entry key="key">value</entry>
7. 
8.</properties>

 

 

2. dispatcher-servlet.xml 에 다음과 같은 내용을 추가해준다

util:properties를 사용하기 위해서 선언해주고, util:properties로 properties.xml을 등록한다.

01.<?xml version="1.0" encoding="UTF-8"?>
02.<beans xmlns:util="http://www.springframework.org/schema/util"xsi:schemalocation="   .... 생략 ....
05. 
06.<!-- properties.xml -->
07.<util:properties id="config"location="classpath:/conf/properties.xml">
08.</util:properties>
09.</beans>

 

 

 

 

3. SpEL을 이용해서 Java에서 사용하는 방법이다.

@Value("#{config['key']}") String picturePath;

 

4. applicationContext.xml과 같은 *.xml에서 사용하는 방법이다

 

1.<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
2.<property name="driverClassName" value="#{config['key']}">
3.... 생략 ...
4.</property></bean>

 

5. JSP에서 사용하는 방법

 

1.<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2.... 생략 ...
3.<spring:eval expression="@config['key']">
4.</spring:eval>

 
====================================================================================================
 
http://seongilman.tistory.com/27
 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "
http://java.sun.com/dtd/properties.dtd">
<properties>
 <comment>common_code</comment>
 <!-- Common Code Definition -->
 <entry key="code.response.success">200</entry>
 <entry key="code.response.error">400</entry>
</properties>

1. xml
<property name="driverClassName" value="#{config['key']}">
 
2. java
@Value("#{code['code.response.error']}")
private String CODE_RESPONSE_ERROR;
 
3. JSP
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
... 생략 ...
<spring:eval expression="@config['key']">
</spring:eval>
====================================================================================================
 
- 확장
 
http://egloos.zum.com/kwon37xi/v/4818439
 
 
parent-properties.xml
<properties>
    <entry key="project.name">Spring Properties Inheritance</entry>
    <entry key="project.home">${java.io.tmpdir}/project</entry>
    <entry key="key.dir">${java.io.tmpdir}/keys</entry>
</properties>

child-properties.xml
<properties>
    <entry key="__extends__">classpath:/parent-properties.xml</entry>
    <entry key="project.name">Sub project</entry> <!-- override parent's project.name -->
    <entry key="subproject.home">${project.home}/subproject</entry>
    <entry key="userproject.home">${project.home}/users/${user.name}</entry> <!-- user.name from System Properties -->
</properties>
 
스프링에서 사용할 때는 다음과 같이한다.
<bean id="configurationProperties" class="kr.pe.kwonnam.properties.InheritablePropertiesFactoryBean">
    <property name="location" value="classpath:/child-properties.xml" />
</bean>
 

spring-properties-inheritance-master.zip
0.00MB

====================================================================================================
- legacy
    @Bean(name = "config")
    public PropertiesFactoryBean config() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("config/config.properties"));
        return bean;
    }
    @Bean(name = "file")
    public PropertiesFactoryBean file() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("config/file.properties"));
        return bean;
    }
====================================================================================================
- SpEL에서 Bean 메소드 사용하기
 
http://springsource.tistory.com/117

xml 설정
<bean id="happyDataSource"
    class="com.happyhouse.rednics.tutorial.spring.property.HappyDataSource">
    <property name="username" value="#{decryptor.getDecryptedUserName()}" />
    <property name="password" value="#{decryptor.getDecryptedPassword()}" />
</bean>
 
Decryptor 클래스 작성
package com.happyhouse.rednics.tutorial.spring.property;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class Decryptor {
    
    // 암호화된 아이디를 주입받는다
    @Value("#{config['jdbc.username']}")
    private String username;
    
    // 암호화된 비밀번호를 주입받는다.
    @Value("#{config['jdbc.password']}")
    private String password;
    
    // 복호화된 아이디를 리턴한다.
    public String getDecryptedUserName() {
        return "decrypted" + username;
    }
    
    // 복호화된 비밀번호를 리턴한다.
    public String getDecryptedPassword() {
        return "decrypted" + password;
    }
}
 
====================================================================================================
 

Posted by 張's blog
,