在grails项目中src/java下的java文件中我想读取conf下的配置文件,怎么做啊
1个回答
展开全部
给你一个java读grails的Config.groovy的封装类,不懂再问我。
import java.util.Map;
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsApplication;
public class ReadProperty {
private final static GrailsApplication grailsApplication = new DefaultGrailsApplication();
public static void main(String[] args) {
System.out.println("grails.views.gsp.encoding=" + getPropertyValue("grails.views.gsp.encoding"));
System.out.println("must.be.null=" + getPropertyValue("must.be.null"));
}
public final static String getPropertyValue(String key) {
String[] keys = key.split("\\.");
if (keys.length == 0)
return "";
Object value = grailsApplication.getConfig().getProperty(keys[0]);
if (keys.length == 1)
return String.valueOf(value);
for (int i = 1; i < keys.length; i++) {
if (value instanceof Map) {
value = ((Map) value).get(keys[i]);
} else {
return null;
}
}
return String.valueOf(value);
}
}
import java.util.Map;
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsApplication;
public class ReadProperty {
private final static GrailsApplication grailsApplication = new DefaultGrailsApplication();
public static void main(String[] args) {
System.out.println("grails.views.gsp.encoding=" + getPropertyValue("grails.views.gsp.encoding"));
System.out.println("must.be.null=" + getPropertyValue("must.be.null"));
}
public final static String getPropertyValue(String key) {
String[] keys = key.split("\\.");
if (keys.length == 0)
return "";
Object value = grailsApplication.getConfig().getProperty(keys[0]);
if (keys.length == 1)
return String.valueOf(value);
for (int i = 1; i < keys.length; i++) {
if (value instanceof Map) {
value = ((Map) value).get(keys[i]);
} else {
return null;
}
}
return String.valueOf(value);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询