Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using registerShutdownHook() in the Spring Framework

Tags:

java

spring

xml

I am following this tutorial online to the letter. http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm

But I get the errors when using Eclipse when I get to this line: context.registerShutdownHook();

Eclipse says:

"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();
    }
}
like image 801
Alexander Ryan Baggett Avatar asked Nov 13 '12 18:11

Alexander Ryan Baggett


1 Answers

For the error it seems that context is a object of ApplicationContext, whereas in tutorial it should be an object of AbstractApplicationContext

I am just guessing that you wrote this

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();
   }
}
like image 59
Ankur Avatar answered Nov 15 '22 20:11

Ankur