Posted on 2010-03-18 01:11
BZ 閱讀(1579)
評論(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_NAME和BY_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);
}
|