1

Can Spring bean be a reference to another bean? It doesn't seem to work:

<bean id="player1" class="some.package.Player" />
<bean id="player2" ref="player1" />

If this is possible, is it useful? So far, I was thinking that every bean must represent a unique object, but some idea came to my head: what if I had a Singleton class called e.g. Sun with a "public static Sun getInstance()" method and I would make two beans being the same object?

<bean id="sun1" class="some.package.Sun" factory-method="getInstance" />
<bean id="sun2" class="some.package.Sun" factory-method="getInstance" />
Wojtek
  • 1,288
  • 11
  • 16
  • related: http://stackoverflow.com/questions/2515367/static-factory-method-spring (not a 100% duplicate) –  Sep 06 '16 at 14:42
  • This is called aliasing. check http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ and search for alias – DwB Sep 06 '16 at 14:52
  • related: [Is spring default scope singleton or not?](http://stackoverflow.com/q/31629993/217324) – Nathan Hughes Sep 06 '16 at 14:54

2 Answers2

2

Looking at the below

<bean id="player1" class="some.package.Player" />

<bean id="player2" ref="player1" />

I think you want to refer same bean with two or more names. If so, you can have comma separated list or ids as (alias concept)

<bean id="player1, player2" class="some.package.Player" />

Now you can refer the bean with either player1 or player2.

Similarly,

<bean id="sun1" class="some.package.Sun" factory-method="getInstance" /> <bean id="sun2" class="some.package.Sun" factory-method="getInstance" />

can be replaced with

<bean id="sun1, sun2" class="some.package.Sun" factory-method="getInstance" />

and this allows you to refer the same bean with two different ids.

And to understand when aliasing is useful, quoting from https://vladmihalcea.com/why-i-like-spring-bean-aliasing/,

Bean aliasing allows us to override already configured beans and to substitute them with a different object definition. This is most useful when the bean definitions are inherited from an external resource, which is out of our control.

Community
  • 1
  • 1
  • Does not answer the question, is that true: sun1.getInstance().hashCode() == sun2.getInstance().hashCode() ? I think it will refer to the same object as design pattern singleton in Java is created on class loader level. – agilob Sep 06 '16 at 14:50
  • @agilob Assuming the `getInstance()` is singleton implementation, yes hashCode will be same and both refer to same object. – Madhusudana Reddy Sunnapu Sep 06 '16 at 14:53
  • It has to be the same because class is `public static class` <- I assume that what class implementation would be if author provided it. – agilob Sep 06 '16 at 14:54
  • `public static class` has nothing to do with how many instances can be created. Also `public static class` will only work for inner beans and not top level beans. – M. Deinum Sep 07 '16 at 05:51
0

Beans are generally singletonic, so they represent one object per context. Standard singleton design pattern in java creates one object per class loader. I think in your case you will re-create one object twice, once with Spring beans, and once with own class loader executed implementation. So both suns will be the same when you compare their hash codes via .getInstance().hashCode().

agilob
  • 6,082
  • 3
  • 33
  • 49