Spring无法解析属性

时间:2016-05-21 07:40:30

标签: spring spring-mvc

我正在使用spring 4并使用PropertySourcesPlaceholderConfigurer获取2个配置文件。我的xml是:

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <tx:annotation-driven />

    <context:component-scan 
        base-package="com.a2bnext.controller,com.a2bnext.dao,com.a2bnext.service,com.a2bnext.util"/>
    <mvc:annotation-driven />

    <bean id="propertyConfigurer"
        class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/database.properties</value>
                <value>/WEB-INF/app.properties</value>               
            </list>
        </property> 
        <property name="location" value="/WEB-INF/database.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="${jdbc.initialSize}" />
        <property name="maxActive" value="${jdbc.maxActive}" />
        <property name="minIdle" value="${jdbc.minIdle}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="hibernateProperties">
          <props>
             <prop 
             key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
             <prop key="hibernate.show_sql">true</prop>
             <prop key="format_sql">true</prop>
             <prop key="use_sql_comments">true</prop>
             <prop key="use_jdbc_metadata_defaults">false</prop>
          </props>
       </property>
       <property name="packagesToScan" value="com.a2bnext.entity" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean> -->

     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
     <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
    <bean id="tilesViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles3.TilesView
            </value>
        </property>
        <property name="order" value="0"/>
    </bean>
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1048576"/>
    </bean>
</beans>

在我正在使用的服务类中:

package com.a2bnext.service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;

import com.a2bnext.dao.A571DAO;
import com.a2bnext.dao.ReceivingNumberDAO;
import com.a2bnext.entity.A571;
import com.a2bnext.entity.ReceivingNumber;


@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Value("${supporting.documents.location}")
    private String supportingDocumentsLocation;


    @Override
    @Transactional
    public Map getResponse(MultipartFile multipartFile) throws IOException {
        System.out.println("Documents location is :"+supportingDocumentsLocation);
        HashMap<String, Object> map = new HashMap<String, Object>();
        Long size = multipartFile.getSize();
        String originalFileName=multipartFile.getOriginalFilename();
        String contentType = multipartFile.getContentType();
        InputStream inputStream = multipartFile.getInputStream();
        byte[] bytes = IOUtils.toByteArray(inputStream);
        if(size>(5*1024*1024)) 
        {
            map.put("fileoriginalsize", size);
            map.put("msg", "failed");
            map.put("reason", "File size cannot be greater than 5MB");
            return map;
        }
        String newFileName=generateFileName();
        // Create the file on server
        File serverFile = new File("D:"+File.separator+"Uploads"+ File.separator + newFileName);
        BufferedOutputStream outputStream = new BufferedOutputStream(
                new FileOutputStream(serverFile));
        outputStream.write(bytes);
        outputStream.close();

        map.put("fileoriginalsize", size);
        map.put("contenttype", contentType);
        map.put("base64", new String(Base64Utils.encode(bytes)));
        map.put("name", newFileName);
        return map;
    }
}

我的app.properties文件:

  

supports.documents.location = D:\ uploads

然而,当我启动应用程序时,我收到错误:

  

无法解析字符串值“$ {supporting.documents.location}”中的占位符'supports.documents.location'

请说明我收到错误的原因。谢谢。

1 个答案:

答案 0 :(得分:1)

您定义locations属性,但随后使用location属性覆盖它。所以在app.properties中没有加载任何内容:

    <property name="locations">
        <list>
            <value>/WEB-INF/database.properties</value>
            <value>/WEB-INF/app.properties</value>               
        </list>
    </property> 
    <property name="location" value="/WEB-INF/database.properties" />