10

我正在网上认真学习本教程。 http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm

但是当我到达这一行时使用 Eclipse 时出现错误: context.registerShutdownHook();

日食 说:

“此行有多个标记 - 语法错误,插入“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();
    }
}
4

6 回答 6

23

对于错误,似乎 context 是 的对象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();
   }
}
于 2012-11-13T18:55:01.480 回答
2

您应该根据 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...
 }
}
于 2014-04-04T06:16:23.253 回答
1

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

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
    HelloWorld obj =(HelloWorld)context.getBean("helloWorld");
    obj.getMessage();
    ((AbstractApplicationContext) context).registerShutdownHook();
}
于 2018-02-01T07:41:59.400 回答
0

//使用这一行,

((AbstractApplicationContext) ctx).registerShutdownHook();

于 2015-02-25T19:37:50.020 回答
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();
    }

}
于 2014-04-09T19:44:47.470 回答
0

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

import org.springframework.context.support.AbstractApplicationContext;  

((AbstractApplicationContext) appContext).registerShutdownHook();
于 2016-06-26T04:00:53.363 回答