Hi,
I am new to developing web apps with java and Spring Framework. I have read about thread safety being an important point that needs to be considered while using java for web application development and have fairly understood the reasons behind it. However, while doing the actual coding, I am unable to figure out which beans will be Singleton and which will not.
In my case, Jaxb2Marshaller is used for working with xml data in a REST application. A Customer class annotated as @XmlRootElement is as follows -
And the controller action is as followsCode:@XmlRootElement(name="customer") @XmlAccessorType(XmlAccessType.FIELD) public class Customer implements RequestNode { private List<String> validationErrors; @XmlElement(name="name") private String name; @XmlElement(name="email") private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Boolean validate() { this.validationErrors = new ArrayList<String>(); if(this.name == null || this.name.equals("")){ this.validationErrors.add("101"); } if(this.email == null || this.email.equals("")){ this.validationErrors.add("102"); } if(this.validationErrors.isEmpty()){ return true; } return false; } @Override public List<String> getValidationErrors() { return this.validationErrors; } }
Since the Customer class instance is being created by jaxb2Mashaller , its not clear whether an instance variable in the Customer class will lead to thread safety issues.Code:@RequestMapping(method=RequestMethod.POST, value="/generate") public String generate(@RequestBody String body, Model model){ Source source = new StreamSource(new StringReader(body)); Customer c = (Customer) this.jaxb2Mashaller.unmarshal(source); //TODO generate response and add response obj to model return "mytemplate"; }
So in general my question is, while using Spring, is it possible to tell in which classes instance variables can be used and where they need to be avoided ?
Thanks


Reply With Quote
