spring boot java原型arraylist

ymdaylpp  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(381)

我正在尝试用JavaSpringBootDo原型模式来保存arraylist。我想实现有arraylist,我可以从任何地方访问,修改它。我得到了什么结果。每次空数组列表。

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PrototypeBean {

    private List<Long> listLong = new ArrayList<>();

    public List<Long> getListLong() {
        return listLong;
    }

    public void setListLong(List<Long> listLong) {
        this.listLong = listLong;
    }

    public PrototypeBean() {}

    public void printData() {

        System.out.println("List size: " + listLong.size());
    }
}

我怎么用这个?

class Calling {

      @Lookup
      public PrototypeBean get(){
           return null;
      }
      private void buildList(){
         List<Long> a = new ArrayList<>();
         a.add(1L);
         a.add(2L);
         get().setListLong(a);
         get().setListLong(a)
         System.out.println(get().getListLong());
 }

}

我也试着从其他班级的名单
类生成列表{

@Lookup
      public PrototypeBean get(){
           return null;
      }
      private void checkList(){
          List<Long> a = new ArrayList<>();
          a.add(1L);
          a.add(2L);
          get().setListLong(a);
          get().setListLong(a)
          System.out.println(get().getListLong());
 }

每次请求我的列表大小总是空的。什么答案,我希望我需要修改列表从各地在同一个请求调用。

flvtvl50

flvtvl501#

请尝试下列操作之一:

@Component
@org.springframework.web.context.annotation.RequestScope
public class YourBean {
    ...
}

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class YourBean {
    ...
}

相关问题