Search code examples
javaspringspring-mvcspring-bootjavabeans

Working of Spring Bean in the following code - How are the Bean properties saved/managed?


I am tring to implement logging of users in Spring Security on the basis of a popular Tutorial and have a doubt about how spring beans are wired.

The below class is defined as a standard bean in the Spring Context

public class ActiveUserStore {

    public List<String> users;

    public ActiveUserStore() {
        users = new ArrayList<String>();
    }

    public List<String> getUsers() {
        return users;
    }

    public void setUsers(List<String> users) {
        this.users = users;
    }
}

The above is defined as a Bean through

@Bean
public ActiveUserStore activeUserStore(){
    return new ActiveUserStore();
}

And the Bean is being used in the below class, please note the users.add(user.getUsername());

@Component
public class LoggedUser implements HttpSessionBindingListener {

private String username; 
private ActiveUserStore activeUserStore;

public LoggedUser(String username, ActiveUserStore activeUserStore) {
    this.username = username;
    this.activeUserStore = activeUserStore;
}

public LoggedUser() {}

@Override
public void valueBound(HttpSessionBindingEvent event) {
    List<String> users = activeUserStore.getUsers();
    LoggedUser user = (LoggedUser) event.getValue();
    if (!users.contains(user.getUsername())) {
        users.add(user.getUsername());//HOW IS THIS SAVED TO THE ACTIVEUSERSTORE BEAN
    }
}

@Override
public void valueUnbound(HttpSessionBindingEvent event) {
    List<String> users = activeUserStore.getUsers();
    LoggedUser user = (LoggedUser) event.getValue();
    if (users.contains(user.getUsername())) {
        users.remove(user.getUsername());//HOW IS THIS SAVED TO THE ACTIVEUSERSTORE BEAN
    }
}

My Question: Since users variable belongs to the ActiveUserStore Bean, how does the following line of code inside the valueBound and valueUnbound methods of Logged User Class, automatically save the ussers data inside the ActiveUserStore Bean ?

users.add(user.getUsername());

I have also marked this line in the code snipped above for clarity.

Any help is appreciated, since I think my understanding of how Beans are wired and used is probably not clear since I am not able to understand the above working. Thanks.


Solution

  • Think about Spring Bean as usual Java class. This class instantiated by Spring at application start just once (actually this is not always true, but by default it is)

    Now you inside of your bean, you have reference to List variable and get it with:

    List<String> users = activeUserStore.getUsers();
    

    Now users variable contains reference to the List stored in bean. This is because in Java objects are passed by reference.

    LoggedUser user = (LoggedUser) event.getValue();
    if (!users.contains(user.getUsername())) {
    

    and here users variable contains List class reference. List class have method add, and LoggedUser have getUsername method. As users variable refers to same List object as your bean property contain, this adds username to your bean:

        users.add(user.getUsername());
    }