2012-12-18 600 views
2

我使用Spring 3.1.1.RELEASE。我无法访问包含在JAR文件中的属性文件。我有这个结构的JAR ...如何访问JAR文件中的属性?

/encryption.properties 
/META-INF/spring/applicationContext-user.xml 

在 “的applicationContext-user.xml” 中可以获得的文件我有

<!-- Define hashing properties --> 
<bean id="localPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>classpath:encryption.properties</value> 
    </property> 
</bean> 
... 
<beans:bean id="jasyptStringDigester" class="org.jasypt.digest.StandardStringDigester"> 
    <beans:property name="algorithm"> 
     <beans:value>${digest.algorithm}</beans:value> 
    </beans:property> 
    <beans:property name="provider"> 
     <beans:ref bean="bcProvider" /> 
    </beans:property> 
    <beans:property name="saltGenerator"> 
     <beans:ref bean="saltGenerator" /> 
    </beans:property> 
    <beans:property name="saltSizeBytes" value="${salt.bytes.length}" /> 
</beans:bean> 

我包括我的WAR文件上面的JAR(在WEB-INF/lib目录下),当我部署我的战争,我得到的错误

08:39:41,796 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-4) Destroying singletons in org.s[email protected]129a92f: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,accountDao,accessCodeDaoImpl,SBTrainerDaoImpl,trainingLinkDAOImpl,trainingSessionDAOImpl,opportunityDao,orderDao,orderLineItemDAO,myclientOrganizationDaoImpl,POForOpportunityDAO,quoteLineItemDaoImpl,SBPersonDaoImpl,trainingAssignmentDao,trainingLocationDaoImpl,accessCodeServiceImpl,opportunitiesService,orderService,trainingSessionService,standardsDaoImpl,standardsServiceImpl,stateDaoImpl,organizationTypeDaoImpl,countryDaoImpl,addressDaoImpl,organizationDao,addressServiceImpl,organizationTypeServiceImpl,organizationService,stateServiceImpl,countryServiceImpl,contractDaoImpl,productDaoImpl,contractServiceImpl,productServiceImpl,coreDaoImpl,roleDaoImpl,userDaoImpl,userServiceImpl,roleServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,localPropertyConfigurer,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,bcProvider,jasyptStringDigester,saltGenerator,passwordEncoder,applicationProperties,dataSource,entityManagerFactory,sharedEntityManager,transactionManager,myclientClient,updateOrdersJob,updateOrdersCronTrigger,updatePDJob,updatePDCronTrigger,org.springframework.scheduling.quartz.SchedulerFactoryBean#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 
08:39:41,802 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-4) Context initialization failed: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'jasyptStringDigester' defined in class path resource [META-INF/spring/applicationContext-user.xml]: Could not resolve placeholder 'digest.algorithm' 
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:220) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:84) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:656) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE] 
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.10.Final.jar:] 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.10.Final.jar:] 
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.0.Final.jar:7.1.0.Final] 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [classes.jar:1.6.0_37] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [classes.jar:1.6.0_37] 
    at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_37] 

所以我的问题是,我该如何正确地引用我的财产JAR的应用程序上下文文件文件?

+0

也许试试这个:http://stackoverflow.com/questions/1599732/springs-propertyplaceholderconfigurer-with-property-in-a-jar-file – Stefan

回答

1

我通过在propertyConfigurer bean中设置“ignoreUnresolvablePlaceholders”属性解决了这个问题。

<!-- Define hashing properties --> 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:encryption.properties</value> 
     </list> 
    </property> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
</bean> 

我摇滚!我的意思是,谷歌和其他人在摇滚之前提出这个问题。

0

我觉得你的位置应该是:

classpath*:encryption.properties 

由于文件驻留在JAR文件,而不是WAR文件的类路径。将*放在告诉配置器在所有类路径中搜索文件。

+0

谢谢,但遗憾的是没有奏效。 – Dave