这个例子在实际应用中是比较有用的,用来将配置文件(*.properties)或者系统属性中的指定属性名称的值加载进来。此例是和Spring结合使用的,所以其他配置就略过了。
1、定义注解@Property
- packagecom.kute.test.selfannotation;
- importjava.lang.annotation.ElementType;
- importjava.lang.annotation.Retention;
- importjava.lang.annotation.RetentionPolicy;
- importjava.lang.annotation.Target;
- /**
- *自定义注解@Property
- *加载配置文件和相应的属性
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.TYPE,ElementType.METHOD})
- public@interfaceProperty{
- //属性名称
- Stringname()default"";
- }
2、解析注解
- packagecom.kute.test.selfannotation;
- importjava.lang.reflect.Method;
- importjava.util.Properties;
- importorg.slf4j.Logger;
- importorg.slf4j.LoggerFactory;
- importorg.springframework.beans.BeansException;
- importorg.springframework.beans.factory.InitializingBean;
- importorg.springframework.beans.factory.config.BeanPostProcessor;
- importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
- importorg.springframework.util.ReflectionUtils;
- publicclassPropertyParseextendsPropertyPlaceholderConfigurerimplements
- BeanPostProcessor,InitializingBean{
- privatestaticLoggerlogger=LoggerFactory.getLogger(PropertyParse.class);
- privatePropertiespros=null;
- //在spring容器实例化bean之后添加自己的逻辑
- publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)
- throwsBeansException{
- returnbean;
- }
- //在spring容器实例化bean之前添加自己的逻辑
- publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)
- throwsBeansException{
- if(bean.getClass().getAnnotation(Property.class)!=null){
- Method[]methods=bean.getClass().getDeclaredMethods();
- for(Methodmethod:methods){
- Propertyp=method.getAnnotation(Property.class);
- if(p!=null){
- //获得对应名称的属性的值
- Objectpara=pros.getProperty(p.name());
- //方法参数类型名称
- StringparameterName=(method.getParameterTypes()[0]).getName();
- if(parameterName.equals("java.lang.Integer")){
- para=newInteger(para.toString());
- }elseif(parameterName.equals(
- "java.lang.Double")){
- para=newLong(para.toString());
- }elseif(parameterName.equals(
- "java.lang.String")){
- para=para.toString();
- }
- logger.info("获得了指定名称的属性的值:"+para);
- ReflectionUtils.invokeMethod(method,bean,
- newObject[]{para});
- }
- }
- }
- returnbean;
- }
- //在初始化bean的时候执行该方法
- publicvoidafterPropertiesSet()throwsException{
- //此方法的作用是将配置的和系统属性加载进来
- pros=mergeProperties();
- }
- }
3、应用注解
- packagecom.kute.test.selfannotation;
- importjava.io.Serializable;
- importorg.slf4j.Logger;
- importorg.slf4j.LoggerFactory;
- @Property
- publicclassTeacherimplementsSerializable{
- privatestaticfinallongserialVersionUID=7651360997972750779L;
- privatestaticLoggerlogger=LoggerFactory.getLogger(Teacher.class);
- @Property(name="third.kute.blog")
- publicvoidsetValue(Stringvalue){
- logger.info("成功设String值:"+value);
- }
- @Property(name="second.kute.age")
- publicvoidsetValue(Integervalue){
- logger.info("成功设Integer值:"+value);
- }
- @Property(name="first.kute.name")
- publicvoidsetValue(Doublevalue){
- logger.info("成功设Double值:"+value);
- }
- }
4、配置文件(部分)(applicationContext.xml)
- <!--PropertyParse此bean是继承了PropertyPlaceholderConfigurer来加载配置文件-->
- <beanclass="com.kute.test.selfannotation.PropertyParse">
- <!--除了支持配置的*.properties外,还支持获取系统属性(System.getProperties()-->
- <propertyname="systemPropertiesModeName"value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
- <!--资源找不到不抛出异常-->
- <propertyname="ignoreResourceNotFound"value="true"/>
- <propertyname="locations">
- <list>
- <value>classpath*:/first.properties</value>
- <value>classpath*:/second.properties</value>
- <value>classpath*:/third.properties</value>
- </list>
- </property>
- </bean>
5、文件内容
first.properties
- first.kute.name=36.0
- first.kute.age=111111
- first.kute.blog=http://blog.youkuaiyun.com/kutekute
second.properties
- second.kute.name=37.0
- second.kute.age=222222
- second.kute.blog=http://blog.youkuaiyun.com/kutekute
third.properties
- third.kute.name=38.0
- third.kute.age=333333
- third.kute.blog=http://blog.youkuaiyun.com/kutekute