PDA

View Full Version : How to wrap a static util class to a Spring bean



siberian
Apr 18th, 2006, 11:04 PM
Hi, I want to configure a util class in spring's config xml.
But all the methods of it are static, can I wrap it to a normal spring bean?

Alarmnummer
Apr 19th, 2006, 01:51 AM
You could do the following:
1) use a MethodInvocationFactoryBean (it can call static methods). You can use it to configure your bean.
2) implement your own FactoryBean that configures your bean (if your beans has a lot of properties, I would use a FactoryBean instead of a bunch of MethodInvocationFactoryBeans).

But you can also redesign your class. If it is configured, it contains state. And my guess is that this util class is some kind of singleton. If this is the case, I would wire up the complete class in Spring, drop all the static methods/fields and change them into 'instance fields' and 'instance methods'. And inject the reference to that instance in every class that needs it. Your class should only contains 'core logic' and it should not contains any 'reference' logic.

siberian
Apr 19th, 2006, 02:14 AM
You could do the following:
1) use a MethodInvocationFactoryBean (it can call static methods). You can use it to configure your bean.
2) implement your own FactoryBean that configures your bean (if your beans has a lot of properties, I would use a FactoryBean instead of a bunch of MethodInvocationFactoryBeans).

But you can also redesign your class. If it is configured, it contains state. And my guess is that this util class is some kind of singleton. If this is the case, I would wire up the complete class in Spring, drop all the static methods/fields and change them into 'instance fields' and 'instance methods'. And inject the reference to that instance in every class that needs it. You class should only contains 'core logic' and it should not contains any 'reference' logic.
Thanks for you suggestion.
the "class" I want to use is from JDK. UiManager, which is unlikely to be redesigned,but I also have some other class written by myself, I will try your suggestion.