無責任的爛筆頭

          Concentrate & enjoy!
          posts - 3, comments - 15, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          AnnotationFormatError when using Spring 3.0 & JavaConfig

          Posted on 2010-03-18 01:11 BZ 閱讀(1583) 評論(0)  編輯  收藏 所屬分類: Spring

              Spring 3.0M3及其以后的版本包含了JavaConfig項目提供的大部分功能. 如果你的程序遭遇如下異常:

          Exception in thread "main" java.lang.annotation.AnnotationFormatError: Invalid default: public abstract org.springframework.beans.factory.annotation.Autowire

           org.springframework.config.java.annotation.Configuration.defaultAutowire()


              這很可能是因為你在Classpath中添加了JavaConfig項目release出來的包(比如org.springframework.config.java-1.0.0.M4.jar), 而在程序中使用JavaConfigApplicationContext類。查看org.springframework.config.java-1.0.0.M4.jar包的source code, 你會發現org.springframework.config.java.annotation.Configuration類的defaultAutowire的定義如下:

          Autowire defaultAutowire() default Autowire.INHERITED;

              而查看org.springframework.beans.factory.annotation.Autowire枚舉類,你會發現INHERITED根本就沒有定義(只有NO, BY_NAMEBY_TYPE三種)。這就難怪會報錯了。

           

              事實上,由于Spring 3.0M3及其以后的版本包含了JavaConfig項目提供的大部分功能,你無需為應用再添加JavaConfig的包。@Configuration, @Bean等都已經被整合到了org.springframework.context.annotation, 從文件來看就是org.springframework.context.jar包。下圖展示了一個可運行項目的Classpath配置:


             由于沒有了JavaConfig項目release的包, JavaConfigApplicationContext類也就無法找到了,你需要將它替換成org.springframework.context.annotation.AnnotationConfigApplicationContext, 例如:

          public static void main(String[] args) {

          ApplicationContext context = new

          AnnotationConfigApplicationContext(ApplicationConfig.class);

          String x = context.getBean("x", String.class);

          System.out.println("Got x: " + x);

          }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 封开县| 东安县| 上思县| 金寨县| 英超| 宜川县| 秦安县| 宁河县| 北京市| 哈巴河县| 潞城市| 靖江市| 临湘市| 柘城县| 萍乡市| 新蔡县| 克东县| 尼玛县| 射阳县| 道孚县| 连江县| 司法| 桂东县| 深泽县| 含山县| 夏津县| 麻江县| 苏尼特左旗| 海门市| 孝昌县| 巴马| 景谷| 利津县| 玉山县| 桂东县| 东乡县| 定州市| 景宁| 敖汉旗| 和平区| 巴彦县|