Created
March 14, 2012 11:33
-
-
Save smat/2035903 to your computer and use it in GitHub Desktop.
Prototype beans in Spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.context.annotation.Scope; | |
import org.springframework.stereotype.Component; | |
@Component | |
@Scope("prototype") | |
public class MyBuilder { | |
public Object build() { | |
return new Object(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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