如何访问JAR文件中的属性?

时间:2012-12-18 14:52:51

标签: spring properties jar classpath applicationcontext

我正在使用Spring 3.1.1.RELEASE。我无法访问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>

我将上面的JAR包含在我的WAR文件中(在其WEB-INF / lib目录中),当我部署WAR时,我收到错误

08:39:41,796 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-4) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@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的应用程序上下文文件中正确引用我的属性文件?

2 个答案:

答案 0 :(得分: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>
我摇滚!我的意思是,谷歌和其他人在摇滚之前提出这个问题。

答案 1 :(得分:0)

我认为你的位置应该是:

classpath*:encryption.properties

由于文件位于JAR文件的类路径中,而不是WAR文件。将*告诉Configurer在所有类路径中搜索文件。

相关问题