2016-05-21 137 views
0

我使用的彈簧4,用2配置files.My XML的PropertySourcesPlaceholderConfigurer是:春無法解決財產

<?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文件:

supporting.documents.location = D:\uploads

但是,當我啓動應用程序時出現錯誤:

Could not resolve placeholder 'supporting.documents.location' in string value "${supporting.documents.location}"

請提出爲什麼我得到的錯誤。謝謝。

回答

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" /> 
+0

謝謝!我早些時候使用它,忘記刪除。感謝很多。 – Jeets