2

このアプリケーションを開発するために Spring MVC を使用しています。外部の .properties ファイルから読み取ることになっています。

mvc-dispatcher-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>

構成.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 になります。

誰かが間違いを指摘できますか?

4

1 に答える 1

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);
    }

}

設定クラスはサービスクラスにします。また、コントローラーでは、サービスクラスオブジェクトを自動配線する必要があります。ここでは、機能しない new 演算子を使用してインスタンスを作成しました。

于 2015-12-14T12:51:17.870 に答える