Announcement Announcement Module
Collapse
No announcement yet.
Beginner Petclinic question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Beginner Petclinic question

    I'm learning Spring (you probably figured that out by the title) and I noticed that the vets ArrayList in the SimpleJdbcClinic exists for the life of the session. It seems like it (and SimpleJdbcClinic, see below) should exist only for the request since I don't see any annotations putting it into the session context. Could someone point out what I'm failing to understand?

    Here is the vets class:
    @XmlRootElement
    public class Vets {

    private List<Vet> vets;

    @XmlElement
    public List<Vet> getVetList() {
    if (vets == null) {
    vets = new ArrayList<Vet>();
    }
    return vets;
    }


    }

    The service:
    @Service
    @ManagedResource("petclinic:type=Clinic")
    public class SimpleJdbcClinic implements Clinic, SimpleJdbcClinicMBean {

    private SimpleJdbcTemplate simpleJdbcTemplate;

    private SimpleJdbcInsert insertOwner;
    private SimpleJdbcInsert insertPet;
    private SimpleJdbcInsert insertVisit;

    private final List<Vet> vets = new ArrayList<Vet>();
    :
    :
    @Transactional(readOnly = true)
    public Collection<Vet> getVets() throws DataAccessException {
    synchronized (this.vets) {
    if (this.vets.isEmpty()) {
    refreshVetsCache();
    }
    return this.vets;
    }
    }
    }

    The controller mapping:

    @RequestMapping("/vets")
    public ModelMap vetsHandler() {
    Vets vets = new Vets();
    vets.getVetList().addAll(this.clinic.getVets());
    return new ModelMap(vets);
    }

    Once the vets list is created it survives multiple requests.

    Thanks
    Last edited by dpantale; Feb 12th, 2013, 07:01 PM. Reason: Added code to make question clearer

  • #2
    Okay, mystery solved. I didn't realize that default scope for ALL beans is singleton. I had the idea that applied only to @controller beans.

    Comment

    Working...
    X