How to use Session Scoped Component in Controller

ε祈祈猫儿з 提交于 2019-12-04 12:50:10

The proxy only intercepts method calls. In your case the following happens:

@Autowired private Count count;

Creates a proxy that looks like an instance of count and therefore also has an i field. But since the proxy is not the real thing, the Count constructor is not called and iremains uninitialized. That's why you always get null.

Now let's introduce a getter:

class Count {
  ...
  public Integer getI() {
    return i;
  }

When you invoke getI() the proxy first checks if there is an instance of the Count bean for the current session. If there is none, one is created. This also means that the Count constructor is called and i is now initalized. Then the proxy delegates the call to the bean's getI() that will return the value of i.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!