inversion of control - Is It Possible To Spring Autowire the same Instance of a protoype scoped class in two places -
** has changed the illustration to better state the situation
I am using and following is the status
@Component @Scope ("prototype") class Foo {} class A {@Autowired Foo fooA; } Class B {@Autowired Foo fooB; } Class C {@Autowired Foo fooC; }
I am trying to understand that if there is any way to use @ etord
and FOO
on
I understand that if there is a scope of FOO
fooB
a different example
Also please explain this is the correct use of the autowiring concept? I'm trying to abuse the Spring Framework purpose
Since neither singleton
and prototype
scopes start to fit you (you do not want an object, but you do not want a new installation every time), you need another chance.
In a web-tool reference is a prepared solution - use the request
field - in this way you will only be an example of your bean in every request / reaction cycle, no matter how many And how often you inject it.
In a non-web application context, you can edit your own implementation of the org.springframework.beans.factory.config.Scope
You can define: After you make it clear, this is a very strange case, what comes to my mind:
- two
factory beans s (actually -
AbstractFactoryBean
) - a new object is returning all the time, and the same object is returned (both toshould be in the area
Foo
s to@resource (name = "prototype factory") and
@Resource (name = "singletonfactoryBean")
(instead of@Autowired
)Singletonfinetben
is a singleton (injected into factory bean square) ) Can be designed to returnprototypefitterbenben
can create a new instance,beanfactor
(getBeanFactory ()
) ViaAutowireCapableBeanFactory and call
.autowire (newlyCreatedBean)
, and then return it. (Alternatively you can injectApplicationContext
and get itsAutowireCapableBeanFactory
) but it is highly complex and you can Extended spring knowledge will be required after my explanation :) Besides this, I think that you should reconsider your design instead of creating the above 'quirks'Updates 2: After your comment, the concept of naming is located in annotations. - I can use
@Resource (name = "someBean") as I indicated above
Comments
Post a Comment