Reputation: 6624
I've realised that refferencing an instance of a spring bean of scope @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
seems to call a new instance of the bean whenever the same instance gets referenced.
For example:
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
class Item {
.....
}
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
class A {
private Item item;
public void setItem(Item item) {
this.item = item;
}
public void method() {
item.doSomething();
}
}
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
class B {
@Autowired
private A a;
@Autowired
private Item item;
void bMethod() {
a.setItem(item);
a.method();
}
}
The instance of A
in a.setItem(item);
seems to be different from that in a.method();
, thereby making it imposible to use accessor methods in beans with scope of @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
.
Is this an expected behaviour? Am I missing some understanding on the usage of @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
?
Upvotes: 2
Views: 192
Reputation: 279930
Get rid of the proxyMode
on your A
bean. With a proxyMode
of TARGET_CLASS
, the bean is actually a proxy which delegates any method invocation to a new instance.
Upvotes: 2