mardi 13 avril 2021

Spring boot Java prototype ArrayList

I am trying with Java spring boot do Prototype pattern which hold ArrayList. I want achieve have ArrayList which I can access from everywhere, modify it. What result I got. Every time empty 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());
    }
}

How I am use this?

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());
 }

}

Also I am trying set list from another's class'es

class BuildList {

      @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());
 }

My list size is always empty per one request. What answer I expect I need modify list from everywhere in same request call.

Aucun commentaire:

Enregistrer un commentaire