读取外部属性文件
Reading external properties file
我正在使用 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。
谁能指出错误是什么?
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);
}
}
配置 class 将其作为服务 class。
在控制器中,您需要自动装配服务 class 对象。
在这里,您已经使用 new 运算符创建了实例,这对您不起作用。
我正在使用 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。
谁能指出错误是什么?
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);
}
}
配置 class 将其作为服务 class。 在控制器中,您需要自动装配服务 class 对象。 在这里,您已经使用 new 运算符创建了实例,这对您不起作用。