Anything like @PreDestroy
in the spring-framework?
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
- Spring Integration - Inbound file endpoint. How to
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
- Spring 5 Web Reactive - Hot Publishing - How to us
You mean like annotating a method with the standard JDK
@PreDestroy
? That's common enough in Spring, and usually better than using adestroy-method
attribute on the bean declaration in XML. All you have to do is includeIn your configuration file and Spring handles the rest.
If you define a bean that implements the DisposableBean interface then Spring will call the
method before destrying the bean.
That's one way, the other is when your bean doesn't have to implement the given interface. In one of yours ConfigurationSupport classes your bean has to be defined as as pulic method with the @Bean annotation.
The method "yourDestroyMethod" has to be defined in YourBean.class and then Spring will call it before destroying the bean.
For more info see the Spring documentation: Destruction callbacks
UPDATE
The third way... I would even say the better way would be to specifiy "init-method" and "destroy-method" of your bean... like this: mkyong.com/spring/spring-init-method-and-destroy-method-example
This solves the problem ot third-party dependency beans, and liberates the the code unnecessary Spring interfaces..
There are 3 ways to do that.
My faivorite is the @PreDestroy method.
To do that u need:
In application-context.xml add the following schema:
The makes the @PreDestroy and the @PostDestroy tags available. Now lets say that you have the ShutDownBean that you want to run some code when the shutdown callback is called. The registers the bean.
Now you are done.
If you have a desktop application then to use the @PreDestroy annotation you need to close it like this:
note: AbstractApplicationContext has the implementation of registerShutdownHook() so this is the minimum class you can use.
Also if you can use the destroy-method tag for classes you do not control their implementation. for example you can add this in your applcation-context.xml:
The destroy-method value can have any visibility but needs to have no arguments.
Hope this helps!
there's standard .NET
IDisposable.Dispose()
method. I don't know Spring but from quick googling it seems that @predestroy is pretty much the same concept.