Quantcast
Channel: Oracle/Java/Others
Viewing all articles
Browse latest Browse all 10

Singleton in Spring by Hongfeng Sun

$
0
0

Follow-up to Singleton in Spring from Oracle/Java/Others

A spring bean default to be singleton. The following class is a Spring bean

Class Transform{
   private Map errors = new HashMap();
   public void setErrors(final Map errors) {
        this.errors = errors;
  }
  public void transform(){
   if(errors.isEmpty()){
    // do something
  } 
}

}

One thread call setErrors() and call transform() to finish it job. The next thread has to remember to call the setErrors() before call transform(), otherwise it will get the values of previous thread.

might safter to change the method signature to

Class Transform{
  public void transform(Errors errors){
   if(errors.isEmpty()){
    // do something
  } 
}
}

So you do not need to call the setErrors(), which is forgettable.


Viewing all articles
Browse latest Browse all 10

Trending Articles