Consider simple 2 cases
1)
@Bean
public MyBean myBean(){
..create and return
}
@Bean
public Another anotherBean(){
new Another(myBean());
myBean();
myBean();
myBean();
}
In such case, some sort of AOP (or not?) will kick in and myBean()
will be called ONLY ONCE
2)
@Component
public class MyClass{
.....
@Autowired
private MyClass self;
@Transactional
public void inTx(){
}
public void doIt(){
inTx(); //this will NOT be transactional
self.inTx(); // this will
}
}
Now, why those 2 cases are different? Why behavior is inconsitent here? I was convinced that in order for AOP in spring to work I ALWAYS have to use scenario 2 and do "external call" on proxied instance. I also thought that scenario 1 will create 4 distinct instances.
So what is the difference and why such inconsistency?
Or maybe scenario 2 is no longer valid because something changed and I just dont know about it?