Question

It says here that

I would not recommend to use InitializingBean and DisposableBean interface, because it will tight coupled your code to Spring

Does it make sense? I thought this would be just the opposite to tight-coupling.

Was it helpful?

Solution

Here the author means that if you let your application classes implement InitializingBean and DisposableBean interfaces (that are spring specific interfaces), then you are coupling your code with spring. In future if spring renames these interfaces (unlikely though), or you stop using spring you will have to update your class code.

Instead if you use init-method and destroy-method attributes in your bean config, your class is independent of spring, i.e. there is no depedency of your class on spring specific classes.

Hope it helps.

OTHER TIPS

I think the idea here is to not create any dependency on your code to spring annotations, see

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

When you explicitly declare this kind of dependency, you're coupling your code to spring jars.

In this other example - http://www.mkyong.com/spring/spring-init-method-and-destroy-method-example/ - it shows how you could use convention methods defined in the XML. No spring imports.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top