Spring mvc如何从.properties中恢复一个值

时间:2016-07-21 10:55:31

标签: spring-mvc properties

我使用Spring MVC。我有一个动态的网络项目。

项目结构

enter image description here

我在/ WEB-INF / classes中有一个correos.properties。

correos.properties是

correosalida = xxx

我想从correos.properties

获取一个属性

调度员

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.jasypt.org/schema/encryption
        http://www.jasypt.org/schema/encryption/jasypt-spring31-encryption-1.xsd">

    <context:annotation-config />
    <import resource="hibernate-context.xml" />
    <context:component-scan base-package="eusurvey" />
    <bean id="propertyPlaceholderConfigurer"
        class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="location" value="/WEB-INF/spring.properties" />
    </bean>

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
    </bean>

    <bean id="environmentVariablesConfiguration"
        class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWITHSHA256AND256BITAES-CBC-BC" />
        <property name="passwordEnvName" value="CAS_PBE_PASSWORD" />
        <property name="providerClassName"
            value="org.bouncycastle.jce.provider.BouncyCastleProvider" />
        <property name="providerName" value="BC" />
    </bean>




    <mvc:annotation-driven />

    <mvc:view-controller path="encuesta/*"
        view-name="encuesta/actualizarCorreoC.jsp" />


    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/classes/messages" />
    </bean>


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/" />



    <context:property-placeholder location="WEB-INF/classes/correos.properties" order="1" ignore-unresolvable="true" />






</beans>

LeerProperties.java是

package eusurvey.auxiliar;

import org.springframework.beans.factory.annotation.Value;

public class LeerProperties {

    @Value("${correos.correosalida}")
    private  String correosalida;

      public  String PropertyValue() {

            return  correosalida;
        }
}

correosalida的值为null。

如何从correos.properties获取属性?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用classpath访问该文件?

应该是这样的:

<context:property-placeholder location="classpath:correos.properties" order="1" ignore-unresolvable="true" />

编辑:

尝试以这种方式加载:

<util:properties id="correos" location="classpath:correos.properties"/>

不应该和#34;#&#34;而不是&#34; $&#34;?

@Value("#{correos.correosalida}")