首页 文章

从我的web-app中加载.properties文件

提问于
浏览
0

我创建了一个我需要在WEB-APP中使用的JAR . 两者都是用spring框架创建的 . 我想在Web应用程序的主上下文中在JAR文件外部加载.properties文件 . 我想用Spring为我们提供的设施来做到这一点 .

我试图在JAR里面的spring.xml文件中做这样的事情:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
       <value>/WEB-INF/classes/my.properties</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
   <property name="triggers">
      <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
         <property name="jobDetail" ref="myJob" />
     <property name="cronExpression" value="${my.cronExpression}"/>
      </bean>
   </property>
</bean>

尝试从my.properties文件加载my.cronExpression . 但没有任何成功 .

我总是得到这个错误:

无法解析占位符'my.cronExpression' .

我尝试使用类路径更改多个变体的位置:/ WEB-INF/classes/my.properties等...

但是我无法加载配置文件 .

谢谢你的帮助 .

3 回答

  • 0

    使用classpath:my.properties - / WEB-INF / classes是类路径的根 .

  • 0

    尝试按如下方式声明:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
           <value>/WEB-INF/classes/my.properties</value>
        </property>
        <property name="ignoreUnresolvablePlaceholders">
           <value>true</value>
        </property>
    </bean>
    
  • 0

    我已经完成了您的代码,并希望您尝试使用此代码段

    这对我来说很有效 :)

    <bean id="placeholderProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:/WEB-INF/classes/my.properties" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="1" />
    </bean>
    

相关问题