I'm trying to get my head around the fact that the spring IoC container holds just only one instance of an object that clients want to be injected with.
Let's define what is a singleton first
An object with mutable states that can only be reached outside of the stack, meaning it is residing in the method memory area (permanent generation space) of the JVM
If you annotation a method in spring with @Bean
shown below:
@Bean
public Student getStudent() {
return new Student();
}
The container will execute this and stores it in the container so that it can inject in constructors like this:
@Autowire
class StudentService() {
public StudentService(Student s) {
this.s = s;
}
}
All fine and dandy. But, isn't this just an another form of a singleton pattern? I mean, sure you have to inject it in order to use it, but what if we have mutable states in the student that clients need and causes an unexpected to show up in another client? Is it common to not have mutable states in a bean and just have methods?
Thank you