2014-12-22 5 views
1

스프링 컨텍스트 내에서 작업하기 위해 노력중인 일부 기존 jar 파일이 있습니다.Spring 컨텍스트에서 * .properties 파일의 절대 경로를 찾으십시오.

내 applicationContext.xml 내부, 내가 사용하는 속성 파일 (들)을로드 :

<context:property-placeholder location="classpath*:META-INF/spring/*.properties" /> 

을 ... 그리고 그것은 동안 스프링 컨텍스트 내에서 완벽하게 작동합니다.

레거시 코드에서 설정 파일의 절대 경로를 얻어야합니다. mvn tomcat:run을 실행할 때와 war 파일로 패키지되고 Tomcat 컨테이너에 배포 될 때 작동해야합니다 (예 : 궁금해하고, 예, 봄, 레거시 코드는 같은 application.properties 설정 파일을 공유합니다.

@Service 
public class Startup {  

    Logger log = LoggerFactory.getLogger(Startup.class); 

    @PostConstruct 
    public void startup() throws Exception { 
     if (!LegacyCode.isInit()){ 
      // this works using a hardcoded path 
      LegacyCode.init("/home/user/absolute/path/to/application.properties"); 
      // this doesn't work, but would the preferred solution if it did 
      // LegacyCode.init("classpath*:META-INF/spring/*.properties"); 
     } 
    } 
} 

은 내가 사용하는 것으로 간주했습니다 다음

String config[] = { "classpath*:META-INF/spring/applicationContext.xml" }; 
    ApplicationContext ctx = new ClassPathXmlApplicationContext(config); 

및 ctx.getResource를 사용하여 경로를 납치하지만, 그냥 응용 프로그램을 얻을 수의 ApplicationContext를 두 번째로로드하는 것은 매우 비효율적 점 외에 .properties '절대 경로를 사용하면 @PostConstruct가 무한 루프로 실행됩니다.

레거시 코드는 (필자가 볼 수 있고 종속성 오류를 기반으로하는) Commons Configuration을 사용하여 구성을 설정합니다. 내가 찾고있는 것은 Commons Configuration이 올바른 application.properties 파일을로드하는 방법입니다 등 WAR 파일 또는 임베디드 톰캣에서, 리눅스, 윈도우에서 실행

+1

Spring XML에는 여러 개의 등록 정보 파일이 있지만 LegacyCode는 하나만 허용합니다. – yunandtidus

+0

그게 옳은데, 레거시 물건에 필요한 구성은 jar1을 위해 필요합니다. 특정 구성이 필요하고 jar2에 대해 다른 구성이 필요합니다. 둘 다 Commons Configuration을 사용하고 Spring은 모두 필요합니다. 조금씩 테스트 해보고 답을 얻습니다. –

답변

2

당신 legacyCode 스프링 자원 ("classpath*:META-INF/spring/applicationContext.xml")

을 이해하지 않는 적절한 파일 경로를 원하고 있다는 것

나는 PERSONNALY 할 것 다음 :

LegacyCode.init(new File(this.getClass().getClassLoader().getResource("application.properties").toURI()).getPath()); 

getResource("application.properties")은 (

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" /> 
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" /> 

후 클래스를

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 

@Configuration 
@PropertySource("classpath:mypropfile.properties") 
public class Config { 
    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

을 만들이 콩을 추가하고보다 당신의 변수를

@Value에 주석을 봄의 맥락에서 봄 표기

+0

작품 100 %, 이것이 내가 결국 사용하는 것입니다 :.. LegacyCode.init (새 파일 (this.getClass() getClassLoader를()의 getResource ( \t \t \t \t "META-INF/봄/application.properties") .toURI()). getPath()); 감사합니다 yunandtidus !! –

0

의 자바와 동일 "$ {cert.path}") 전용 문자열 certPath;