Is Spring Autowire possible to have the same protoype instance in two places

** changed the example to better express the situation.

I am using spring 2.5 and have the following situation

@Component @Scope("prototype") Class Foo { } class A { @Autowired Foo fooA; } class B { @Autowired Foo fooB; } class C { @Autowired Foo fooC; } 

I'm trying to figure out if there is a way to use @Autowired and bind the same instance of FOO to fooA and fooB when binding another instance to fooC

I understand that if the FOO area is singleton , it will work

but I wander if there is a way to achieve the same goal using the protoype .

Also explain if this use of the auto concept is correct? i am trying to abuse spring wireframe target

+4
source share
3 answers

Since neither singleton nor prototype tags seem to suit you (you don't need one object, but you don't want every new instance every time), you need a different scope.

In the context of the web application, there is a ready-made solution - use the request area - so in each request / response cycle you will have only one instance of your bean, regardless of where and how many times you enter it.

In the context of a non-web application, you can define your own implementation of org.springframework.beans.factory.config.Scope

Update: after you find out, this seems like a very strange case. The following occurs to me:

  • define two FactoryBean (in fact - subclasses of AbstractFactoryBean ) - each returning a new object each time and one returning the same object (both of them should be in singleton )
  • type Foo with @Resource(name="prototypeFactoryBean") and @Resource(name="singletonFactoryBean") (instead of @Autowired )
  • singletonFactoryBean can be designed to simply return a singleton (introduced in the factory bean class)
  • prototypeFactoryBean can create a new instance, drop the BeanFactory (accessible via getBeanFactory() ) to AutowireCapableBeanFactory and call .autowire(newlyCreatedBean) and then return it. (alternatively you can enter ApplicationContext and get it AutowireCapableBeanFactory )

But this is too complicated, and you will need advanced knowledge of spring even after my explanation :)

Also, I think you should rethink your design instead of doing the above quirks.

Update 2: After your comment, the concept of naming is transferred to the annotations - as indicated above, you can use @Resource(name="someBean")

+5
source

The whole point of the prototype is that every time you get a different instance.

In addition, the auto-authorization of a bean with a prototype is doubtful, constructive (in fact, I would be slightly surprised if it were even allowed). The usual idea is to combine the beans together with the same area (there are ways around this, but not relevant here).

Everything about your design suggests that Foo should not be a prototype - why did you do it?

+1
source

Objects designed for the Autowiring prototype are possible, but a new instance is created each time. Therefore, to answer your question: no, you cannot do this.

Your use of component scanning and auto-installation seems acceptable for another part.

+1
source

Source: https://habr.com/ru/post/1300173/


All Articles