본문 바로가기
컴퓨터관련

Spring properties 사용

by 기록이답이다 2016. 3. 22.
반응형

아래에서 작성한 Spring xml 파일을 읽어들여 JSoup으로 Parsing하기 방법을 이용하여 spring에서 properties 파일을 읽어들여 static method에서 사용하였다.


우선 설정파일이다.



1
2
3
4
5
/spring/g.properties
 
 
value1=설정값1 (value1=\uC124\uC815\uAC121)
value2=설정값2 (value2=\uC124\uC815\uAC122)
실제 properties 파일에서 한글은 저런식으로 변환되어서 저장된다. bean을 이용하여 java에서 읽어들이기 위해 xml을 설정한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
context-common.xml
<!--?xml version="1.0" encoding="UTF-8"?-->
     
    <!--?xml:namespace prefix = "context" /--><context:component-scan base-package="bugnote">
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">
    </context:exclude-filter>  
    <bean class="org.springframework.core.LocalVariableTableParameterNameDiscoverer">
 
    <bean id="prop" class="bugnote.common.Prop">
        <property value="classpath:/spring/g.properties" name="propFile">
    </property>
         
</bean>
</bean></context:component-scan></beans>
Prop 클래스는 prop 라는 id로 생성이 되며 setPropFile 이라는 메소드를 가지고 있다. Prop 클래스는 아래와 같다.
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
Prop.java
 
package bugnote.common;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
 
@Service("prop")
public class Prop {
    private Properties _properties;
     
    public void setPropFile(String proFile) throws IOException {
        FileSystemResourceLoader fileSystemResourceLoader = new FileSystemResourceLoader();
        Resource propResource = fileSystemResourceLoader.getResource(proFile);
        InputStream is = propResource.getInputStream();
         
        _properties = new Properties();
        _properties.load(is);
    }
     
    public String get(String key) {
        return _properties.getProperty(key);
    }
}
setPropFile을 보면 Spring xml 파일을 읽어들여 JSoup으로 Parsing하기 의 getDocument 메소드와 많이 비슷한것을 알 수 있다. inputStream을 얻어 properties를 loading 하여 java에서 사용할 수 있도록 한다. 이제 static 메소드에서 사용하는 일만 남았다.
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
32
33
BugnoteConst.java
 
package bugnote.common;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class BugnoteConst {
    @Autowired
    @Resource(name="prop")
    public Prop prop;  
     
    @PostConstruct
    public void init() {
        BugnoteConst.BUGNOTE_VALUE1 = prop.get("value1");
        BugnoteConst.BUGNOTE_VALUE2 = prop.get("value2");
    }
     
    public static String BUGNOTE_VALUE1;
    public static String BUGNOTE_VALUE2;
     
    public static final int SEARCH_RESULT_TEXT = 1;
    public static final int SEARCH_RESULT_XML = 2;
    public static final int SEARCH_RESULT_JSON = 3;
    public static final int SEARCH_TYPE_INTEGRATE = 4;
                       ...
                       ...
}
위 static 메소드에서 @PostConstruct anotation을 적용한 init 메소드에서 prop클래스로부터 값을 읽어와 static 변수에 설정한다. 이때 static 변수는 값을 설정해줘야 하기 때문에 final을 제거한다. 사용은 아래와 같이 하면 된다.
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
32
33
34
35
36
37
38
39
40
41
MainController.java
 
package kr.ictee.nsic.main;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import kr.ictee.nsic.common.NsicConst;
import kr.ictee.nsic.common.NsicUtil;
import kr.ictee.nsic.common.PageUtil;
import kr.ictee.nsic.model.BbsVO;
import kr.ictee.nsic.model.NSReguDataVO;
import kr.ictee.nsic.repository.entity.ConvertMap;
import kr.ictee.nsic.service.IWebService;
import net.sf.json.JSONArray;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class MainController extends BaseController {
     
    //메인 리스트
    @RequestMapping("/main.do")
    public ModelAndView main(HttpServletRequest req, HttpServletResponse res) throws Exception {
        ModelAndView mav = new ModelAndView();
         
                logger.info("value1 = " + BugnoteConst.BUGNOTE_VALUE1); // 이와 같이 일반적인 방법으로 사용하면 된다.
 
        mav.setViewName("tiles.main");
          
        return mav;
    }
}
물론 ("#{prop['value1']") String value1 이런식으로 처리를 해도 되지만 static 변수에서는 적용이 되지 않아 bean을 생성후 위와 같이 처리를 했다. xml 대신에 properties로 처리한 이유는 .... 그냥 ㅎㅎ 그냥 그렇게 하고 싶었다.
반응형