Using Spring init and destroy lifecycle callback in non-web apps
When using spring to create singleton classes, we often have to use some initialing and teardown mechanisms to acquiring and releasing resources in the program, such as opening and closing files, using connections to databases, etc.
As spring recommends default constructor to ensure the testability of your class, it is necessary to use an init method and teardown method in the class. Although spring provides serveral interfaces(such as InitializingBean and DisposableBean) for programmers to implement, it is to intrusive for a program to couple with spring when a simple init method is introduced.
So spring provides XML-based configuration for this. You only have to declare the init and teardown method in bean definition in xml configuration file, such as
`
And spring also provides default-init-method=”" and default-destroy-method=”" on beans node, which will be invoked if specified method is available.
Posted in 技术