请问一个关于Spring MVC的问题
在spring的配置文件中我们可以用<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>xxxx.properties</value>
</property>
</bean>
然后在配置文件中通过${jdbc.xxx}来直接引用配置文件中的变量
请问基于注解的controller 如
@RequestMapping("/admin")
这里的"/admin"可以通过读properties实现吗?
我通过 一个static final String XXXX;然后用static{读配置文件}
是不行的。
望知道的朋友解答下,谢谢 --------------------编程问答-------------------- 我没用过spring mvc , 你要用static 读取配置文件 ???
我不知道咋回事。
你可以定义 一个 PropertyManager类 专门封装 获取 属性,删除属性等一系列操作
我贴下 代码;
package com.chenghui.common.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
/**
* This class is implemented as a singleton since many classloaders seem to
* take issue with doing classpath resource loading from a static context.
*/
public class PropertyManager {
private static PropertyManager manager = null;
private static Object managerLock = new Object();
private static String propsName = "/application.properties";
private Properties properties = null;
private Object propertiesLock = new Object();
private String resourceURI;
/**
* Creates a new PropertyManager. Singleton access only.
*/
private PropertyManager(String resourceURI) {
this.resourceURI = resourceURI;
}
/**
* Returns one property.
*
* @param name the name of the property to return.
* @return the property value specified by name.
*/
public static String getProperty(String name) {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
return manager.getProp(name);
}
/**
* Sets a Yazd property. If the property doesn't already exists, a new
* one will be created.
*
* @param name the name of the property being set.
* @param value the value of the property being set.
*/
public static void setProperty(String name, String value) {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
manager.setProp(name, value);
}
/**
* Deletes a Yazd property. If the property doesn't exist, the method
* does nothing.
*
* @param name the name of the property to delete.
*/
public static void deleteProperty(String name) {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
manager.deleteProp(name);
}
/**
* Returns the names of the Yazd properties.
*
* @return an Enumeration of the Yazd property names.
*/
public static Enumeration propertyNames() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
return manager.propNames();
}
/**
* Returns true if the properties are readable. This method is mainly
* valuable at setup time to ensure that the properties file is setup
* correctly.
*/
public static boolean propertyFileIsReadable() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
return manager.propFileIsReadable();
}
这个是别人写的 hoho~~~
回复内容过长 ,·~~删除一些些·~~ --------------------编程问答-------------------- 补上~~
--------------------编程问答-------------------- 非常感谢您的回答。
/**
* Returns true if the properties are writable. This method is mainly
* valuable at setup time to ensure that the properties file is setup
* correctly.
*/
public static boolean propertyFileIsWritable() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
return manager.propFileIsWritable();
}
/**
* Returns true if the yazd.properties file exists where the path property
* purports that it does.
*/
public static boolean propertyFileExists() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsName);
}
}
}
return manager.propFileExists();
}
/**
* Gets a Yazd property. Yazd properties are stored in yazd.properties.
* The properties file should be accesible from the classpath. Additionally,
* it should have a path field that gives the full path to where the
* file is located. Getting properties is a fast operation.
*
* @param name the name of the property to get.
* @return the property specified by name.
*/
protected String getProp(String name) {
//If properties aren't loaded yet. We also need to make this thread
//safe, so synchronize...
if (properties == null) {
synchronized (propertiesLock) {
//Need an additional check
if (properties == null) {
loadProps();
}
}
}
String property = properties.getProperty(name);
if (property == null) {
return null;
} else {
return property.trim();
}
}
/**
* Sets a Yazd property. Because the properties must be saved to disk
* every time a property is set, property setting is relatively slow.
*/
protected void setProp(String name, String value) {
//Only one thread should be writing to the file system at once.
synchronized (propertiesLock) {
//Create the properties object if necessary.
if (properties == null) {
loadProps();
}
properties.setProperty(name, value);
saveProps();
}
}
protected void deleteProp(String name) {
//Only one thread should be writing to the file system at once.
synchronized (propertiesLock) {
//Create the properties object if necessary.
if (properties == null) {
loadProps();
}
properties.remove(name);
saveProps();
}
}
protected Enumeration propNames() {
//If properties aren't loaded yet. We also need to make this thread
//safe, so synchronize...
if (properties == null) {
synchronized (propertiesLock) {
//Need an additional check
if (properties == null) {
loadProps();
}
}
}
return properties.propertyNames();
}
/**
* Loads Yazd properties from the disk.
*/
private void loadProps() {
properties = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream(resourceURI);
properties.load(in);
} catch (Exception e) {
System.err.println("Error reading Yazd properties in PropertyManager.loadProps() " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
}
/**
* Saves Yazd properties to disk.
*/
private void saveProps() {
//Now, save the properties to disk. In order for this to work, the user
//needs to have set the path field in the properties file. Trim
//the String to make sure there are no extra spaces.
String path = properties.getProperty("path").trim();
OutputStream out = null;
try {
out = new FileOutputStream(path);
properties.store(out, "bonuesrule.properties -- " + (new java.util.Date()));
} catch (Exception ioe) {
System.err.println("There was an error writing yazd.properties to " + path + ". "
+ "Ensure that the path exists and that the Yazd process has permission " + "to write to it -- "
+ ioe);
ioe.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
}
}
}
/**
* Returns true if the properties are readable. This method is mainly
* valuable at setup time to ensure that the properties file is setup
* correctly.
*/
public boolean propFileIsReadable() {
try {
InputStream in = getClass().getResourceAsStream(resourceURI);
return true;
} catch (Exception e) {
return false;
}
}
/**
* Returns true if the yazd.properties file exists where the path property
* purports that it does.
*/
public boolean propFileExists() {
String path = getProp("path");
if (path == null) {
return false;
}
File file = new File(path);
if (file.isFile()) {
return true;
} else {
return false;
}
}
/**
* Returns true if the properties are writable. This method is mainly
* valuable at setup time to ensure that the properties file is setup
* correctly.
*/
public boolean propFileIsWritable() {
String path = getProp("path");
File file = new File(path);
if (file.isFile()) {
//See if we can write to the file
if (file.canWrite()) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
不过读配置文件不是问题了,关键是在怎么在注解中使用配置文件中的值 --------------------编程问答-------------------- 非常感谢您的回答。
不过读配置文件不是问题了,关键是在怎么在注解中使用配置文件中的值 --------------------编程问答-------------------- 注解起到标示的作用 --------------------编程问答-------------------- 这个也不太清楚,你可以用SpEL 表达式试一试。
配置一个Map类型的bean。
<util:map key-type="java.lang.String" value-type="java.lang.String" id="requestMappingMap">
<entry key="admin" value="/admin" />
</util:map>
可以把@RequestMapping(value = "#{ requestMappingMap['admin'] }")
我这里的表达式是对map做投影运算,表达式语法,你可以参考Spring的文档。
至于@RequestMapping 是否支持SpEL表达式,我也没有验证过。 你试一试吧。
如果是@Value标注的话,那肯定是可以的。 --------------------编程问答-------------------- 如果我没有记错的话,SpEL是spring3.0以后才有的。 --------------------编程问答-------------------- 试了下,不行,不过非常感谢你的回答。谢谢 --------------------编程问答-------------------- --------------------编程问答-------------------- 期待更好的回信
补充:Java , Java EE