项目会经常解析properies文件,根据key获得value,下面写一个如何使用注解的方式来获得value
1. AnnotateProperties类:用来获得properties文件的路径
package AnnotateSrc;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotateProperties {
String value();
String key() default "";
}
2.Property类:用来记录键值对
package AnnotateSrc;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Property {
String key();
String defaultValue() default "";
}
3.Annotate:JavaBean使用注解的方式声明properties文件存放的路径
package test;
import AnnotateSrc.AnnotateProperties;
import AnnotateSrc.Property;
[color=red]@AnnotateProperties(value = "test.properties", key = "C:\\")[/color]
public class Annotate {
@Property(key = "header")
private String header_;
public String getHeader() {
return header_;
}
public void setHeader(String header) {
this.header_ = header;
}
}
4.Test :测试类
package test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import AnnotateSrc.AnnotateProperties;
import java.util.Properties;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
AnnotateProperties annotationProperties = Annotate.class
.getAnnotation(AnnotateProperties.class);
String propertiesName = annotationProperties.value();
String dir = annotationProperties.key();
System.out.println(dir+propertiesName);
File f = new File(dir+"\\"+propertiesName);
Properties pro = readProperties(f);
System.out.println(pro.get("header"));
}
private static Properties readProperties(File file) throws Exception {
BufferedInputStream bis = null;
Properties prop = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
prop = new NullableProperties();
prop.load(bis);
} catch (FileNotFoundException ex1) {
throw new Exception(ex1);
} catch (SecurityException e) {
throw new Exception(e);
} catch (Exception ex2) {
throw new Exception(ex2);
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (Exception ex) {
}
}
return prop;
}
public static class NullableProperties extends Properties {
public String getProperty(String key, String defaultValue) {
String origin = super.getProperty(key, defaultValue);
if (origin != null && origin.trim().length() == 0) {
// 空白
return defaultValue;
}
return origin;
}
};
}
分享到:
相关推荐
java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解...
NULL 博文链接:https://smallbee.iteye.com/blog/1611962
自己写的一些关于java注解使用的例子,感觉里面写的挺全了,希望对大家的学习有帮助.有不足之处的话还请多多指教.
这是自己总结的java注解 使用了思维导图,方便读取,快速阅读
java注解和反射的个人学习笔记
Java注解实现
Java注解的使用例子,使用Ant编译,执行run.bat运行.
使用Java自定义注解来拼接SQL查询语句,方便使用。
主要包括:普通java注解,hibernate注解,spring注解,struts2注解,对于学习注解的朋友有很大的帮助!
Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解
简述java注解功能及用法,有代码示例。看起来还成,比较简单,学spring注入的时候要用到注解注入的话,本书属于必看版。
主要介绍了Java注解使用及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
入门级java 注解学习示例代码
包括Java自定义注解以及包括自定义注解的应用实例。以及注解相关知识的注解
关于java注解的使用
Java 注解(Annotation) - 请认准 ih0qtq
java注解解析知识要点 精简易懂
一、枚举类的使用: 1.枚举类的理解:类的对象只有有限个,确定的。我们称此为枚举类 2.当需要定义一组常量时,强烈建议使用枚举类。...五、注解(Annotation)概述 二、常见的Annotation 示例: 六、JDK中的元
java 自定义注解验证 可自己添加所需要的注解,本案例中只写了三个自定义注解类 本案例只花了半天时间,如有不好之处请多提提意见。谢谢!
Java 注解技术详解