shadowfox
shadowfox

Reputation: 581

AbstractApplicationContext vs ApplicationContext

what is the difference between AbstractApplicationContext and ApplicationContext ? can we call

context.registerShutdownHook()  

using ApplicationContext?

I saw this while going through a sample code -

public static void main(String[] args) {
    AbstractApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");
    context.registerShutdownHook();
}

Upvotes: 14

Views: 11235

Answers (3)

Srikanth Popuri
Srikanth Popuri

Reputation: 97

registerShutdownHook() is not part of ApplicationContext. So, we can not use Application context.

This method can be invoked using references using with either ConfigurableApplicationContext or AbstractApplicationContext.

As methods can be called either from interface or class having the implementation. Because, we actually create object for ClassPathXmlApplicationContext using the reference of AbstractApplicationContext.

Difference: ConfigurableApplicationContext is an interface where the methods are implemented in AbstractApplicationContext class.

Upvotes: 2

Arun Kumar Mudraboyina
Arun Kumar Mudraboyina

Reputation: 779

registerShutdownHook() gracefully shutdowns bean and preform finalization like calling the destroy methods. This is the method declared in the interface ConfigurableApplicationContext which is implemented by AbstractApplicationContext,and it is not implemented by ApplicationContext.So the invokation of registerShutdownHook() only possible from the AbstractApplicationContext's object

Upvotes: 9

Aravind Yarram
Aravind Yarram

Reputation: 80176

Same as the diff between abstract class (AbstractApplicationContext ) and an interface (ApplicationContext).

Can we call context.registerShutdownHook() using ApplicationContext?

No, because registerShutdownHook() is part of ConfigurableApplicationContext interface which is not extended by ApplicationContext

Upvotes: 15

Related Questions