spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.61k stars 38.13k forks source link

FactoryBean, Question [SPR-10137] #14770

Closed spring-projects-issues closed 11 years ago

spring-projects-issues commented 11 years ago

Manfred Quasten opened SPR-10137 and commented

As I understood, FactoryBean is only useable for Prototype or Singleton Scope.

What is about other beans, session, request or customerScopes?

I have create a proxyFactory to create setters and getters for domain objects and want that the beans will be managed by spring. In JSF I can use the SpringELResolver, to use springBeans. The idea was, to create the proxies with factoryBean. The targetClass of the web models can be configered in springs applicationContext. It could be so nice, but I do not believe, that FactroyBean can do this job. All what I've read didn't make hope ...

What is the concept behind this FactoryBean? Why is it only be useable for Singeltons and Prototypes? Has it been forgotten to support the other scopes? At the beginning only singelton and Prototype was supported in spring, as I know.

Ok it can be possible to use some hacks using RequestContextHolder to make it running, but I don't like doing it, becuase it is trial and error ...

What is the way to create beans in other scopes? How can I plant my proxies to the beanfactory. JSF is a way, but managed beans from JSF don't support autowiring, and I do not want to use it. May be it is an idea to use the maps from jsf (request,session,view) to put the beans in and return it from there, than it works, only for jsf, but it is simple to understand ...

if( ! facesContext.allMaps.contains(keyFromClass(targetClass)) ) { allMaps.put(keyFromClass(targetClass),proxyFactory.create(targetClass.newInstance())) }

return allMaps.get(keyFromClass(targetClass));

I think supporting all scopes by factoryBean would be very nice or. For my understanding, it would encapsulate the complete technical shit and it will be the only way to make it independend from used environment ...
So it can be an improvement and I Think it can be great to integrate Spring with Webframeworks.


No further details from SPR-10137

spring-projects-issues commented 11 years ago

Chris Beams commented

Hi Manfred,

Unfortunately, our JIRA issue tracker isn't really the place for usage questions like this one. In the future please consider posting to StackOverflow or the SpringSource forums.

With that said, I'd recommend taking a look at Spring's @Configuration and @Bean support, which provide you with complete programmatic control over bean creation logic as well as full scoping support. This functionality is really designed for use by application developers, whereas the FactoryBean contract is really more an adapter layer that allows arbitrarily complex bean creation logic not otherwise possible in Spring beans XML. We don't typically recommend that users implement FactoryBeans these days—@Configuration classes and @Bean methods are almost always a nicer fit.

spring-projects-issues commented 11 years ago

Manfred Quasten commented

Hi Chris,

thanks for the hint about Configuration. I've tried it and it works and it is a realy easy way to plaint my Proxies.

spring-projects-issues commented 11 years ago

Chris Beams commented

Great!