`
yanfaguanli
  • 浏览: 650699 次
文章分类
社区版块
存档分类
最新评论

JAVA自定义注解(2)

 
阅读更多


这个例子在实际应用中是比较有用的,用来将配置文件(*.properties)或者系统属性中的指定属性名称的值加载进来。此例是和Spring结合使用的,所以其他配置就略过了。

1、定义注解@Property

  1. packagecom.kute.test.selfannotation;
  2. importjava.lang.annotation.ElementType;
  3. importjava.lang.annotation.Retention;
  4. importjava.lang.annotation.RetentionPolicy;
  5. importjava.lang.annotation.Target;
  6. /**
  7. *自定义注解@Property
  8. *加载配置文件和相应的属性
  9. */
  10. @Retention(RetentionPolicy.RUNTIME)
  11. @Target({ElementType.TYPE,ElementType.METHOD})
  12. public@interfaceProperty{
  13. //属性名称
  14. Stringname()default"";
  15. }

2、解析注解
  1. packagecom.kute.test.selfannotation;
  2. importjava.lang.reflect.Method;
  3. importjava.util.Properties;
  4. importorg.slf4j.Logger;
  5. importorg.slf4j.LoggerFactory;
  6. importorg.springframework.beans.BeansException;
  7. importorg.springframework.beans.factory.InitializingBean;
  8. importorg.springframework.beans.factory.config.BeanPostProcessor;
  9. importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
  10. importorg.springframework.util.ReflectionUtils;
  11. publicclassPropertyParseextendsPropertyPlaceholderConfigurerimplements
  12. BeanPostProcessor,InitializingBean{
  13. privatestaticLoggerlogger=LoggerFactory.getLogger(PropertyParse.class);
  14. privatePropertiespros=null;
  15. //在spring容器实例化bean之后添加自己的逻辑
  16. publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)
  17. throwsBeansException{
  18. returnbean;
  19. }
  20. //在spring容器实例化bean之前添加自己的逻辑
  21. publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)
  22. throwsBeansException{
  23. if(bean.getClass().getAnnotation(Property.class)!=null){
  24. Method[]methods=bean.getClass().getDeclaredMethods();
  25. for(Methodmethod:methods){
  26. Propertyp=method.getAnnotation(Property.class);
  27. if(p!=null){
  28. //获得对应名称的属性的值
  29. Objectpara=pros.getProperty(p.name());
  30. //方法参数类型名称
  31. StringparameterName=(method.getParameterTypes()[0]).getName();
  32. if(parameterName.equals("java.lang.Integer")){
  33. para=newInteger(para.toString());
  34. }elseif(parameterName.equals(
  35. "java.lang.Double")){
  36. para=newLong(para.toString());
  37. }elseif(parameterName.equals(
  38. "java.lang.String")){
  39. para=para.toString();
  40. }
  41. logger.info("获得了指定名称的属性的值:"+para);
  42. ReflectionUtils.invokeMethod(method,bean,
  43. newObject[]{para});
  44. }
  45. }
  46. }
  47. returnbean;
  48. }
  49. //在初始化bean的时候执行该方法
  50. publicvoidafterPropertiesSet()throwsException{
  51. //此方法的作用是将配置的和系统属性加载进来
  52. pros=mergeProperties();
  53. }
  54. }

3、应用注解
  1. packagecom.kute.test.selfannotation;
  2. importjava.io.Serializable;
  3. importorg.slf4j.Logger;
  4. importorg.slf4j.LoggerFactory;
  5. @Property
  6. publicclassTeacherimplementsSerializable{
  7. privatestaticfinallongserialVersionUID=7651360997972750779L;
  8. privatestaticLoggerlogger=LoggerFactory.getLogger(Teacher.class);
  9. @Property(name="third.kute.blog")
  10. publicvoidsetValue(Stringvalue){
  11. logger.info("成功设String值:"+value);
  12. }
  13. @Property(name="second.kute.age")
  14. publicvoidsetValue(Integervalue){
  15. logger.info("成功设Integer值:"+value);
  16. }
  17. @Property(name="first.kute.name")
  18. publicvoidsetValue(Doublevalue){
  19. logger.info("成功设Double值:"+value);
  20. }
  21. }

4、配置文件(部分)(applicationContext.xml)
  1. <!--PropertyParse此bean是继承了PropertyPlaceholderConfigurer来加载配置文件-->
  2. <beanclass="com.kute.test.selfannotation.PropertyParse">
  3. <!--除了支持配置的*.properties外,还支持获取系统属性(System.getProperties()-->
  4. <propertyname="systemPropertiesModeName"value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
  5. <!--资源找不到不抛出异常-->
  6. <propertyname="ignoreResourceNotFound"value="true"/>
  7. <propertyname="locations">
  8. <list>
  9. <value>classpath*:/first.properties</value>
  10. <value>classpath*:/second.properties</value>
  11. <value>classpath*:/third.properties</value>
  12. </list>
  13. </property>
  14. </bean>

5、文件内容

first.properties

  1. first.kute.name=36.0
  2. first.kute.age=111111
  3. first.kute.blog=http://blog.csdn.net/kutekute

second.properties

  1. second.kute.name=37.0
  2. second.kute.age=222222
  3. second.kute.blog=http://blog.csdn.net/kutekute

third.properties

  1. third.kute.name=38.0
  2. third.kute.age=333333
  3. third.kute.blog=http://blog.csdn.net/kutekute
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics