2012-11-13 51 views
9

我正在關注本教程的在線教程。 http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm在Spring框架中使用registerShutdownHook()

,但我得到使用Eclipse 當錯誤,當我得到這一行: context.registerShutdownHook();

Eclipse中說:

「在這一行 多個標記 - 語法錯誤,插入 「AssignmentOperator表達」 完成 分配 - 語法錯誤,插入 「;」 來完成陳述 - 方法registerShutdownHook() ApplicationContext「

我正在關注本教程。我所有的變量名都完全一樣。我的代碼和他完全一樣。我不知道什麼是錯的。

我在做什麼錯,可以做些什麼來解決這個問題,以便我可以繼續教程。

package com.tutorialspoint; 

import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp 
{ 
    public static void main(String[] args) 
    { 
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); 

     HelloWorld obj = (HelloWorld)context.getBean("helloWorld"); 
     obj.getMessage(); 
     context.registerShutdownHook(); 
    } 
} 
+1

你可以發佈你的代碼片段嗎? – Ankur

+0

'code' package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; 公共類MainApp { \t公共靜態無效的主要(字串[] args) \t { \t \t ApplicationContext的上下文= \t \t \t \t新的ClassPathXmlApplicationContext(「豆。XML 「); \t \t \t \t \t 的HelloWorld \t OBJ =(的HelloWorld)context.getBean(的」 HelloWorld「); \t \t obj.getMessage(); \t \t context.registerShutdownHook(); \t \t \t} \t \t \t \t }'code' –

+0

正如我所說的,你正在使用'ApplicationContext'而不是'AbstractApplicationContext' – Ankur

回答

20

有關錯誤似乎這方面是ApplicationContext一個對象,而在教程應該是AbstractApplicationContext

我只是猜測,你寫了這個

public class MainApp { 
    public static void main(String[] args) { 

     ApplicationContext context = 
          new ClassPathXmlApplicationContext("Beans.xml");//error here 

     HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); 
     obj.getMessage(); 
     context.registerShutdownHook(); 
    } 
} 
+0

好的,需要什麼導入才能使用AbstractApplicationContext?現在它告訴我,它不能被解析爲一種類型。 –

+0

只需按Ctrl + Shift + o如果您正在使用eclipse或'import org.springframework.context.support.AbstractApplicationContext' – Ankur

+0

非常感謝您的工作=) –

1

你的對象根據spring文檔應該使用AbstractApplicationContext代替ApplicationContext http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-nature

import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public final class Boot { 

public static void main(final String[] args) throws Exception { 
    AbstractApplicationContext ctx 
     = new ClassPathXmlApplicationContext(new String []{"beans.xml"}); 

    // add a shutdown hook for the above context... 
    ctx.registerShutdownHook(); 

    // app runs here... 

    // main method exits, hook is called prior to the app shutting down... 
} 
} 
0

這是我工作的代碼。

package arjun; 

import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.FileSystemResource; 

public class Main { 

    public static void main(String[] args) { 

       AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring.xml"); 
       context.registerShutdownHook(); 
       Triangle triangle=(Triangle) context.getBean("triangle"); 
       triangle.draw(); 
    } 

} 
0

//使用這條線,

((AbstractApplicationContext)CTX).registerShutdownHook();

0

這裏有一個更新的解決方案:

import org.springframework.context.support.AbstractApplicationContext; 

((AbstractApplicationContext) appContext).registerShutdownHook(); 
1

我也得到了同樣的問題。我用這種方法解決了它。

public static void main(String[] args) { 
    ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml"); 
    HelloWorld obj =(HelloWorld)context.getBean("helloWorld"); 
    obj.getMessage(); 
    ((AbstractApplicationContext) context).registerShutdownHook(); 
} 
+0

不要忘記導入(import org.springframework.context.support.AbstractApplicationContext;) – Susampath