無責任的爛筆頭

          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 閱讀(1582) 評論(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);

          }


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


          網站導航:
           
          主站蜘蛛池模板: 黄石市| 遵义市| 平阴县| 乌兰察布市| 张家港市| 七台河市| 沙坪坝区| 额尔古纳市| 四会市| 亚东县| 八宿县| 山丹县| 安多县| 富顺县| 巫溪县| 黄平县| 信宜市| 泗阳县| 宿迁市| 荃湾区| 理塘县| 即墨市| 宝坻区| 来宾市| 阿荣旗| 鲁甸县| 含山县| 隆昌县| 平顺县| 沙洋县| 温宿县| 常山县| 义马市| 武山县| 芮城县| 永修县| 鸡东县| 延津县| 临清市| 梨树县| 连南|