2012-07-10 6 views
0

나는 봄을 사용하고 있습니다. 외부화 된 특성 파일이 있습니다. 나는 아래와 같이로드하고있다.세션 범위에서 속성 값을 유지하는 방법은 무엇입니까?

<context:property-placeholder location="file:///C:/some.properties"/> 

어떻게 키 - 값 쌍으로 세션의 속성을 유지할 수 있습니까?

ServletContextListener를 확장하는 수신기를 작성하려고했습니다.

public class Sample implements ServletContextListener { 
@Override 
    public void contextInitialized(ServletContextEvent event) { 
//here i tried to get the values of properties file as below. 
InputStream stream = event.getServletContext().getResourceAsStream("C:\\some.properties"); 
//But here stream is coming as null 


} 

} 

여기에 아무 것도 없나요?

감사합니다.

답변

2

SetvletContextcontextInitlalized()는 서블릿 컨텍스트가 초기화 될 때 호출 될 때 성공적으로 응용 프로그램이로드,

당신이

event.getServletContext().setAttribute("global_properties", propertiesInstance); 

만약에 넣을 수있는 속성은 애플리케이션 컨텍스트에 파일을 저장하려면

당신은 그것을 세션 당 원하는 다음 HttpSessionListenersessionCreated() 방법

그래서 그렇게 자주 사용되는 데이터를 넣어 acros s의 응용 프로그램은 applicationscope이고 세션에 제한되어 있지만 자주 사용되는 데이터는 넣으십시오. session

+0

답장 보내 주셔서 감사합니다. 외부화 된 특성 파일을로드 한 후 호출되는 contextInitlalized()입니까? 감사! – user1016403

+0

당신은 환영합니다 (내가 정말로 귀하의 질문에 대답하면 upvote 수/답변으로 허용 :) –

1

ServletContextListner와 통신하는 PropertyPlaceHolderConfigurer를 사용하는 것이 좋습니다. 이 PropertyPlaceHolderConfigurer 클래스는 processProperties를 호출하여 모든 속성의 맵을 얻을 수 있습니다.

@Override 
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, 
    Properties props) throws BeansException { 
    super.processProperties(beanFactoryToProcess, props); 
    resolvedProps = new HashMap<String, String>(); 
    for (Object key : props.keySet()) { 
     String keyStr = key.toString(); 

     resolvedProps.put(keyStr, parseStringValue(props.getProperty(keyStr), props, 
       new HashSet())); 
    } 
} 

그리고 청취자 contextInitialized에

()는 당신이 할 수처럼 : ExposablePropertyPlaceHolder이 PropertyPlaceHolderConfigurer는을 확장하는 클래스가

ServletContext servletContext = sce.getServletContext(); 
    WebApplicationContext context = WebApplicationContextUtils 
      .getRequiredWebApplicationContext(servletContext); 
    ExposablePropertyPlaceHolder configurer =(ExposablePropertyPlaceHolder)context.getBean(propertiesBeanName); 
    sce.getServletContext().setAttribute(contextProperty, configurer.getResolvedProps()); 

.

관련 문제