itun
itun

Reputation: 3521

Injection of one instance to several beans

There is one solution:

    <bean name="1" class="My1" />

<bean name="2" class="My2" scope="prototype">
    <property name="field1" ref="1">
</bean>

<bean name="3" class="My3" scope="prototype">
    <property name="field1" ref="1">
</bean>

But I don't want to do that. I don't want that bean "1" is accessible everywhere in the application and Spring controls it. I only want that bean "2" and "3" get the same instance. Can I achieve that? How?

Upvotes: 0

Views: 114

Answers (2)

Japan Trivedi
Japan Trivedi

Reputation: 4483

What if you don't specify the bean "1" in the xml explicitely and just create its object in the controller first request and then put that in your session so that it can be accessible in the whole application with same instance. And you can access it in only the controller you want.

Hope this helps you.

Cheers.

Upvotes: 0

Tim Pote
Tim Pote

Reputation: 28029

I'm not really sure what the issue is. By default, Spring beans are singletons. So in your example, 2 and 3 already have the same instance of 1. The Spring-controlled instance of 1 is not really "accessible everywhere in the application", it's only accessible where it's been injected.

Upvotes: 4

Related Questions