在Spring Framework中使用registerShutdownHook()

我在线阅读本教程。 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(); } } 

对于错误,似乎上下文是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(); } } 

根据spring文档http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-nature,您应该使用AbstractApplicationContext而不是ApplicationContext。

 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... } } 

我也遇到了同样的问题。 我用这种方式解决了它。

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

这是适合我的代码。

 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(); } } 

//使用这一行,

((AbstractApplicationContext)ctx).registerShutdownHook();

这是一个更新的解决方案:

 import org.springframework.context.support.AbstractApplicationContext; ((AbstractApplicationContext) appContext).registerShutdownHook();