aspose file tools*
The moose likes Spring and the fly likes doubt in Spring : <bean ... init-method=" title="Thanksgiving" /> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "doubt in Spring : <bean ... init-method="" destroy-method="" ...>" Watch "doubt in Spring : <bean ... init-method="" destroy-method="" ...>" New topic
Author

doubt in Spring : <bean ... init-method="" destroy-method="" ...>

Sidhartha Ray
Greenhorn

Joined: Jul 04, 2012
Posts: 29
Hello everybody
I'm new in spring f/w.
I've a doubt in <bean> tag. I'm setting the "init-method" & "destroy-method" to some method(say start() & stop()).
Here the problem is, I'm finding start() method getting executed but the stop() method not, means even if I'm reassigning a reference or explicitly assigning null to it, the stop() method doesn't get executed.

Any idea ???
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
you should post this in the spring forum.

what reference are you assigning null to? the appcontext? if so, it is not enough. call the context's close() method.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

By default Spring beans are singletons. They hang around for the life of the context. As Jesus said calling close on the ApplicationContext will cause these beans to be destroyed. Typically the destroy method is used to clean up a resource or close connections when the application is terminated. In most cases you wont need this.


[How To Ask Questions][Read before you PM me]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Also, the close() method isn't actually on the ApplicationContext interface, it is on the ConfigurableApplicationContext interface, so you will have to cast your ApplicationContext to this interface in order to call close() directly.

Note, if you use XmlWebApplicationContext in a web environment, then close is called automatically when you undeploy or shutdown your web container.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sidhartha Ray
Greenhorn

Joined: Jul 04, 2012
Posts: 29
Thanks all for your response...

coming to Jesus & Bill, I've made that bean's scope="prototype" & then assigning null to it & again also reassigning to it another new object, still the stop() method doesn't get executed........

coming to Mark, I'm not making any web application, it's simply an core java program & I'm creating appl context as,
ApplicationContext context = ClassPathXmlApplicationContext("hello.xml");
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
i think everyone will want to know what object are you assigning null to.

did you try the close method along with casting mentioned above?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

That is because the prototype scope is a little different. Spring does not manage the complete lifecycle of prototype scoped beans. It is the responsibility of the application code to do any expensive clean-up on these. Usually regular garbage collection is sufficient but if you have special needs for clean-up this has to be handled manually.

From the reference docs

In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the
container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client,
with no further record of that prototype instance. Thus, although initialization lifecycle callback methods
are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle
callbacks are not called. The client code must clean up prototype-scoped objects and release expensive
resources that the prototype bean(s) are holding. To get the Spring container to release resources held by
prototype-scoped beans, try using a custom bean post-processor, which holds a reference to beans that
need to be cleaned up.
In some respects, the Spring container's role in regard to a prototype-scoped bean is a replacement for the
Java new operator. All lifecycle management past that point must be handled by the client. (For details on
the lifecycle of a bean in the Spring container, see the section called “Lifecycle callbacks”.)
 
Don't get me started about those stupid light bulbs.
 
subject: doubt in Spring : <bean ... init-method="" destroy-method="" ...>