有时候我们需要将某个字符串类型的枚举名转换为枚举类型,Apache common lang 包里面的EnumUtils可以帮我们做到
maven 依赖,这里我用的是lang3
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2.1</version>
</dependency>
枚举类
public enum Template {
template1("模板一","/WEB-INF/ftl/pobaicn/");
private String name;
private String path;
Template(String name, String path) {
this.name = name;
this.path = path;
}
@Override
public String toString() {
return "[name=" + name + " ,path=" + path + "]";
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
demo
public static void main(String[] args) {
Template template = EnumUtils.getEnum(Template.class,
"template1");
System.out.println(template.getPath());
}
输出
/WEB-INF/ftl/pobaicn/