This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Why only static methods allowed on factory-bean?

Nov 2nd, 2004, 07:41 AM

Hello,

I have a doubt with regard to the new factory-bean factory-method introduced in the 1.1 release.

In general i find it a very good idea but i have found a problem while accessing a method from a bean defined in the container. Is there any reason so that the instantiateUsingFactoryMethod in the AbstractAutowireCapableBeanFactory only searches for static methods?

I would understand this for static factory methods dfined in some classes, but im confused with the line

if (Modifier.isStaticfactoryMethod.getModifiers()) &&

Which forces static attribute on an already context instantiated bean.

In general i find it a very good idea but i have found a problem while accessing a method from a bean defined in the container. Is there any reason so that the instantiateUsingFactoryMethod in the AbstractAutowireCapableBeanFactory only searches for static methods?

As I understand the documentation, that limitation is valid only if the BeanDefinition specifies a class. I understand it's the class to instantiate. So it would not make sense to allow for non-static instances for it would require to instantiate the type before invoking the method.

Comment

Actually I am no Spring developer, so I can only guess.
I would say, the restriction has been made out of design purposes. The idea of a factory bean is actually the creation of an object. To additionally support static factory methods on a bean instance which itself is a factory seems not intuitive. So I do not view this as a real restriction in usage.

Comment

The only problem that migh arise is in the case of some 3rd party library where the method is declared as static but usually called in an object.

But it usually represents a very bad design decissions and programmers should be spanked (for example changing a global static variable from constructor or any other method. The consecuences of allowing that on unprepared hands bring me nightmares, yeeeks!)