3

我正在使用 Spring 3.1.1.RELEASE。我无法访问 JAR 文件中包含的属性文件。我有一个具有这种结构的 JAR……</p>

/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 的应用程序上下文文件中正确引用我的属性文件?

4

2 回答 2

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>

I rock! I mean, Google and other people asking this question before rock.

于 2012-12-20T14:55:16.923 回答
0

我认为您的位置应该是:

classpath*:encryption.properties

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

于 2012-12-19T01:35:34.817 回答