2015-12-14 99 views
2

我正在使用Spring MVC來開發此應用程序。我應該從外部.properties文件讀取。閱讀外部屬性文件

MVC-調度-servlet.xml中

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="locations"> 
      <list> 
       <value>classpath:Host.properties</value> 
       <value>file:///usr/pic1/JSONEditor/Host.properties</value> 
      </list> 
      </property> 
      <property name="ignoreUnresolvablePlaceholders" value="true"/> 
      <property name="ignoreResourceNotFound" value="true"/> 
     </bean> 

<bean id="dataSource" class="com.example.editor.Configuration"> 
      <property name="hostURL" value="${url}" /> 
     </bean> 

Configuration.java

package com.example.editor; 

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

public class Configuration { 

    @Value("$(url)") 
    private String hostURL; 

    public String getHostURL(){ 
     System.out.println("URL:"+this.hostURL); 
     return this.hostURL; 
    } 

    public void setHostURL(String url){ 
     this.hostURL = url; 
     System.out.println("URL:"+this.hostURL); 
    } 

} 

EditorController.java

package com.example.editor.controller; 

import java.io.IOException; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.example.editor.Configuration; 



    @Controller 
    @RequestMapping("/") 
    public class EditorController { 

     private static final String start = "Editor"; 

     @RequestMapping(value = "/", method = RequestMethod.GET) 
     public String start(ModelMap model) throws IOException{ 

     Configuration confg = new Configuration(); 
     model.addAttribute("URL", confg.getHostURL()); 

      return start; 
     } 

    } 

它能夠讀取該文件並獲取URL時我啓動應用程序,但是一旦我在瀏覽器中打開它,hostURL = null。

有人能指出什麼是錯誤嗎?

+3

當然,它是......你正在創建一個新的'配置'的實例,這是春天未觸及的。 –

回答

0
package com.example.editor; 

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


@Service 
public class Configuration { 

    @Value("$(url)") 
    private String hostURL; 

    public String getHostURL(){ 
     System.out.println("URL:"+this.hostURL); 
     return this.hostURL; 
    } 

EditorController.java

package com.example.editor.controller; 

import java.io.IOException; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.example.editor.Configuration; 


    @Autowired 
    Configuration confg; 

    @Controller 
    @RequestMapping("/") 
    public class EditorController { 

     private static final String start = "Editor"; 

     @RequestMapping(value = "/", method = RequestMethod.GET) 
     public String start(ModelMap model) throws IOException{ 

     model.addAttribute("URL", confg.getHostURL()); 

      return start; 
     } 

    } 
    public void setHostURL(String url){ 
     this.hostURL = url; 
     System.out.println("URL:"+this.hostURL); 
    } 

} 

對於配置類使其作爲服務類。 而在控制器中,您需要自動佈線的服務類對象。 在這裏你已經創建了新操作符的實例,它不會爲你工作。

+0

謝謝!這個工作完美! :) – user2522981