軟件工程實踐者

          統計

          留言簿

          友情鏈結

          閱讀排行榜

          評論排行榜

          2016年9月2日 #

          Java 8 Repeatable注解的使用

          Java 8之前,同一注解不能在相同的目標元素上多次使用,例如,如下的注解在Java 8之前是不允許的:
          public class SampleClass {
              
              @Quality("Security")
              @Quality("Performance")
              @Quality("Readability")
              public void foo(){
                  //
              }
          }

          Java 8引入了Repeatable注解(@Repeatable)可以解決這一問題,但光有可重復的注解定義還不夠,還需要它的容器注解,兩者一起來實現可重復注解的使用。實例如下:
          @Target(ElementType.METHOD)
          @Retention(RetentionPolicy.SOURCE)
          @Repeatable (Qualities.class)
          public @interface Quality {
              String value();
          }
          @Target(ElementType.METHOD)
          public @interface Qualities {
              Quality[] value();
          }

          其中,Quality是可重復注解,由@Repeatable注解標明,它的容器注解是Qualities,用于存放所有可重復的Quality(存貯在Quality[]中);同時還要注意可重復注解和它的容器注解的目標元素必須是一樣的(這也不言自明)。如此這般,我們最開始的 SampleClass 在Java 8環境下就可以安全使用了。


          posted @ 2016-09-06 16:24 軟件工程實踐者 閱讀(836) | 評論 (0)編輯 收藏

          Bill Push的單例模式實現

          以下單例實現思想來自《Java Design Patterns: A Programmer's Approach》.
          該方法利用了Java缺省的Lazy類實例化機制克服了傳統單例模式實現中Lazy實例化方式的不足。

          public class Singleton {
             private Singleton(){}
             
             public static Singleton getInstance(){
                return Helper.instance;
             }

             static class Helper {
                 private static Singleton instance = new Singleton();
             }
          }

          posted @ 2016-09-03 09:37 軟件工程實踐者 閱讀(232) | 評論 (0)編輯 收藏

          Maven error “Failure to transfer…”

          以下轉自StackOverflow(http://stackoverflow.com/questions/5074063/maven-error-failure-to-transfer),親測可用。


          This worked for me in Windows as well.

          1. Locate the {user}/.m2/repository (Using Juno /Win7 here)
          2. In the Search field in upper right of window, type ".lastupdated". Windows will look through all subfolders for these files in the directory. (I did not look through cache.)
          3. Remove them by Right-click > Delete (I kept all of the lastupdated.properties).
          4. Then go back into Eclipse, Right-click on the project and select Maven > Update Project. I selected to "Force Update of Snapshots/Releases". Click Ok and the dependencies finally resolved correctly.

          posted @ 2016-09-02 15:52 軟件工程實踐者 閱讀(187) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 阿勒泰市| 禹城市| 东乌珠穆沁旗| 额济纳旗| 博爱县| 淳安县| 遂宁市| 皋兰县| 托里县| 寿光市| 临沧市| 峨边| 穆棱市| 富阳市| 霸州市| 大洼县| 阿坝| 北海市| 宜黄县| 瑞安市| 佳木斯市| 天台县| 洛川县| 兴义市| 白城市| 通州市| 米脂县| 唐河县| 和田县| 漳平市| 黔西| 界首市| 旺苍县| 运城市| 额敏县| 方城县| 会同县| 广安市| 修文县| 汝南县| 会泽县|