在Spring Framework中使用registerShutdownHook()

Ale*_*ett 9 java xml spring

我在线阅读本教程. http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm

但是当我到达这一行时,我在使用Eclipse时遇到了错误:context.registerShutdownHook();

Eclipse说:

"此行的多个标记 - 语法错误,插入"AssignmentOperator Expression"以完成赋值 - 语法错误,插入";"以完成语句 - 方法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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Ank*_*kur 21

对于错误,似乎上下文是一个对象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();
   }
}
Run Code Online (Sandbox Code Playgroud)