2012-02-07 26 views
0

我試圖將一個簡單的Spring 2.5 hello世界項目(從Spring Recipes)轉換爲3.0。對於初學者,我創建了一個簡單的Spring實用程序項目(來自新項目的Spring Template Project選項)。如何將Spring 2.5項目轉換爲3.0

我已將源代碼複製到3.0項目的「src/main/java/com.apress.springrecipes.hello」包中。在那裏,bean配置文件位於「src/main/resources/META-INF/spring/app-context.xml」。但是,2.5 hello world調用文件「beans.xml」,它在項目的根路徑中創建。 Main類的代碼如下所示:

public static void main(String[] args) { 
    ApplicationContext context = 
     new ClassPathXmlApplicationContext("beans.xml"); 

    HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); 
    helloWorld.hello(); 
} 

如何更改此選項以查找新的app-context.xml?

回答

2

我想補充的src/main/resources/META-INF/spring目錄到CLASSPATH,然後改變這樣的代碼:

public static void main(String[] args) { 
    ApplicationContext context = 
     new ClassPathXmlApplicationContext("app-context.xml"); // why would expect to find beans.xml? 

    HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); 
    helloWorld.hello(); 
} 

這種變化無關與Spring版本;它需要你瞭解你的CLASSPATH是什麼以及Spring將加載應用程序上下文XML文件的位置。

您應該瞭解如何利用註釋進行自動佈線。

您應該閱讀Spring 3.1中的new features

您應該更改您的Spring應用程序上下文以指向新版本3.0 .xsd和命名空間。

當然,您需要3.1 JAR。

+0

謝謝。爲了將來的參考,如果有人想在STS中設置類路徑來實現這樣的目標,請選擇Run,Run Configurations。單擊用戶條目,前進,添加外部文件夾,然後深入到配置文件前的路徑。在我的情況下,它是/ SpringRecipesHelloWord/src/main/resources/META-INF/spring。 – 2012-02-07 04:19:14