在Spring Framework中使用registerShutdownHook() [英] using registerShutdownHook() in the Spring Framework
问题描述
我在线阅读本教程。
http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm
I am following this tutorial online to the letter. http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm
但是当我到达这一行时,我在使用Eclipse
时遇到错误:
context.registerShutdownHook();
But I get the errors when using Eclipse when I get to this line: context.registerShutdownHook();
Eclipse说:
此行的多个标记
- 语法错误,插入AssignmentOperator Expression以完成
赋值
- 语法错误,插入;以完成语句
- 方法registerShutdownHook()未定义类型
ApplicationContext
"Multiple markers at this line - Syntax error, insert "AssignmentOperator Expression" to complete Assignment - Syntax error, insert ";" to complete Statement - The method registerShutdownHook() is undefined for the type ApplicationContext"
我正在完全遵循本教程。我的所有变量名都完全相同。我的代码与他的完全相同。我不确定是什么问题。
I am following this tutorial exactly. All of my variable names are exactly the same.My code is exactly the same as his. I am not sure what is wrong.
我做错了什么,可以做些什么来解决这个问题,以便我可以继续教程。
What am I doing wrong, what can be done to fix this so that I can continue the tutorial.
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
For the error it seems that context is a object of ApplicationContext
, whereas in tutorial it should be an object of 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 Framework中使用registerShutdownHook()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!