简体   繁体   English

Spring Bean在以下代码中的工作-如何保存/管理Bean属性?

[英]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. 我正打算根据流行的教程在Spring Security中实现用户日志记录,并且对spring bean的连接方式有疑问。

The below class is defined as a standard bean in the Spring Context 下面的类在Spring Context中被定义为标准bean

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()); 并且在下面的类中使用了Bean,请注意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变量属于ActiveUserStore Bean,因此Logged User Class的valueBound和valueUnbound方法中的以下代码行如何自动将Ussers数据保存在ActiveUserStore Bean中?

users.add(user.getUsername()); 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. 感谢任何帮助,因为我认为我对Bean的连接和使用方式的理解尚不清楚,因为我无法理解上述工作。 Thanks. 谢谢。

Think about Spring Bean as usual Java class. 将Spring Bean视为通常的Java类。 This class instantiated by Spring at application start just once (actually this is not always true, but by default it is) 由Spring在应用程序启动时实例化的此类仅一次(实际上并非总是如此,但默认情况下为true)

Now you inside of your bean, you have reference to List variable and get it with: 现在您进入了bean,您可以引用List变量并使用以下命令获取它:

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

Now users variable contains reference to the List stored in bean. 现在,用户变量包含对存储在bean中的列表的引用。 This is because in Java objects are passed by reference. 这是因为在Java对象中是通过引用传递的。

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

and here users variable contains List class reference. 这里的用户变量包含List类的引用。 List class have method add, and LoggedUser have getUsername method. 列表类具有add方法,而LoggedUser具有getUsername方法。 As users variable refers to same List object as your bean property contain, this adds username to your bean: 当用户变量引用与bean属性包含的相同List对象时,这会将用户名添加到bean中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM