jQuery in Action, 3rd edition
The moose likes Spring and the fly likes Spring - AbstractApplicationContext close() method does not destroy bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring - AbstractApplicationContext close() method does not destroy bean" Watch "Spring - AbstractApplicationContext close() method does not destroy bean" New topic

Spring - AbstractApplicationContext close() method does not destroy bean

Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

In summary:

a. spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
<context:annotation-config />
<context:component-scan base-package="org.tutorial.spring" />

b. SpringJdbcDemo.java

package org.tutorial.spring;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.tutorial.spring.dao.SpringJdbcDao;
import org.tutorial.spring.model.Circle;

public class SpringJdbcDemo {

public static void main(String[] args) {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
SpringJdbcDao dao = ctx.getBean("springJdbcDao", SpringJdbcDao.class);
Circle circle = dao.getCircle(1);


Notice that the ctx.close() is before the dao.getCircle();

c. SpringJdbcDao.java

package org.tutorial.spring.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Component;
import org.tutorial.spring.model.Circle;

public class SpringJdbcDao {

public Circle getCircle(int circleId) {

Connection conn = null;
Circle circle = null;
String derbyDriver = "org.apache.derby.jdbc.ClientDriver";

try {
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/db");
PreparedStatement ps = conn.prepareStatement("select * from circle");
ResultSet rs = ps.executeQuery();

if (rs.next()) {
circle = new Circle(circleId, rs.getString("name"));

} catch (Exception e) {
throw new RuntimeException(e.toString());
} finally {
try {
} catch (SQLException e) {

return circle;


The output:

Feb 24, 2013 9:40:55 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@45e7c8de: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springJdbcDao,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Circle [id=1, name=First Circle

The dao still works after the ctx.close() method.
Any help is appreciated.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 607

Well it's kind of hard to destroy a bean

When the context is closed a bean is destroyed in the sense that any "after" methods get called. The bean can no longer be got from the application context.

However the bean isn't removed from memory - as long someone holds a reference to that bean - you can call methods on that bean. Its just no longer managed by Spring Container ...

The bean is only truly destroyed when it gets garbage collected. That could only happen if there are no active references to that bean!

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
I agree. Here's the link: http://aspose.com/file-tools
subject: Spring - AbstractApplicationContext close() method does not destroy bean
It's not a secret anymore!