Skip to content

Instantly share code, notes, and snippets.

@smat
Created March 14, 2012 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smat/2035903 to your computer and use it in GitHub Desktop.
Save smat/2035903 to your computer and use it in GitHub Desktop.
Prototype beans in Spring
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class MyBuilder {
public Object build() {
return new Object();
}
}
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.stereotype.Component;
@Component
public class MyOtherService {
private ObjectFactory<MyBuilder> myBuilderObjectFactory;
public MyOtherService(ObjectFactory<MyBuilder> myBuilderObjectFactory) {
this.myBuilderObjectFactory = myBuilderObjectFactory;
}
public void doSomething() {
// New instance
MyBuilder myBuilder = myBuilderObjectFactory.getObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment