scope prototype not working

when to use prototype scope in spring
scope(value=prototype not working)
difference between prototype and request scope in spring
spring request scope example
spring boot prototype bean
scoped beans as dependencies
inject request scoped bean into singleton
spring objectfactory

I have the following configuration :

@Configuration
public class GameConfig {
    @Bean(name = "redsox")
    public Team getRedSox() {
        return new Team("RedSox");
    }

    @Bean(name = "boston")
    public Team getBoston() {
        return new Team("Boston");
    }

    @Bean @Scope(value="prototype") 
    public Game getGame(@Qualifier("boston") Team t1, @Qualifier("redsox") Team t2) {
        return new Game( t1, t2 );
    }

    @Bean
    GameController gameController(Game g) {
        return new GameController(g);
    }
}

@RestController
@RequestMapping("game")
public class GameController {

    Game game;

    public GameController(Game game) {
        this.game = game;
    }

    @RequestMapping(path = "play", method = RequestMethod.GET)
    public Team play() {
        return game.play();
    }

    @RequestMapping(path = "setHomeTeam", method = RequestMethod.POST)
    public void setHomeTeam(@RequestBody Team t) {
        game.setHomeTeam(t);
    }
}

public class Game {

    private Team homeTeam;
    private Team awayTeam;

    public Game (Team homeTeam, Team awayTeam){
        this.homeTeam = homeTeam;
        this.awayTeam = awayTeam;
    }

    public Team play (){
        Random randomGenerator = new Random();
        // Generate random integers in the range 0..1
        int randomInt = randomGenerator.nextInt(2);
        if (randomInt == 0 )
            return this.homeTeam;
        else
            return this.awayTeam;
    }

    public void setHomeTeam (Team t){
        this.homeTeam = t;
    }

    public void setAwayTeam (Team t){
        this.awayTeam = t;
    }
}

@Getter  @NoArgsConstructor @AllArgsConstructor
public class Team {
    private String name;
}

I would like the Game not to be singletone. but when I call POST request http://localhost:8080/game/setHomeTeam and then I call GET request http://localhost:8080/game/play it remembers the setHomeTeam.

The only solution I have found was configure gameController in another way:

@Bean  
@Scope(value="prototype")
GameController gameController(
    @Qualifier("boston") Team t1, 
    @Qualifier("redsox") Team t2
) {
    return new GameController(new Game(t1, t2));
}

but this creates the controller + Game as non Singletone. I would like to have only the Game non-Singletone. Is there any better/ efficient way ?


A better way to implement qualifier is with annotations Define 2 annotations :

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface Redsox {
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface Boston {
}

in the config file , change the following beans:

@Bean @Redsox
public Team getRedSox() {
    return new Team("RedSox");
}

@Bean @Boston
public Team getBoston() {
    return new Team("Boston");
}

@Bean @Scope(value="prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)
public Game getGame(@Boston Team t1, @Redsox Team t2) {
    return new Game( t1, t2 );
}

@Scope("prototype") bean scope not creating new bean, The problem arises when we try to wire beans of different scopes. For example, a prototype bean into a singleton. This is known as the scoped bean injection problem. Another way to solve the problem is method injection with the @Lookup annotation. Thanks guys!!! – tintin Oct 1 '11 at 18:26 How does this work if the bean would have had request scope instead of prototype scope. Would you still need to retrieve the bean with context.getBean(..) ? – dr jerry Feb 24 '15 at 10:07 Or use a scoped proxy, i.e. @Scope(value="prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) – svenmeier


@Bean 
@Scope(value="request",proxyMode = ScopedProxyMode.TARGET_CLASS) 
public Game getGame(@Qualifier("boston") Team t1, @Qualifier("redsox") Team t2) {
    return new Game( t1, t2 );
}

If we had interface we would use : ScopedProxyMode.INTERFACES

Look at http://www.baeldung.com/spring-bean-scopes section "4.1. Request Scope" Annotation @RequestScope is equivalent to

@Scope(value="request",proxyMode = ScopedProxyMode.TARGET_CLASS)

Or in short @RequestScope.

If the class (Game) is final this solution is not available, since a proxy calss is created.

Injecting Prototype Beans into a Singleton Instance in Spring , The problem arises when we try to wire beans of different scopes. For example, a prototype bean into a singleton. This is known as the scoped� In this quick article, we're going to show different approaches of injecting prototype beans into a singleton instance. We'll discuss the use cases and the advantages/disadvantages of each scenario. By default, Spring beans are singletons. The problem arises when we try to wire beans of different scopes. For example, a prototype bean into a


Another option is to define in gameController :

@Lookup
public  Game getGameBean(){
    return null;
}

The above is equivalent to context.getBean(Game.class) because you do not have application spring context. and you don't need to define proxyMode in the bean definition

Quick Guide to Spring Bean Scopes, Learn how to quickly navigate the available bean scopes in the Spring framework . Right now, we're not hiring new authors in the Java area – only Linux, A bean with prototype scope will return a different instance every time it is the bean is shared across multiple servlet-based applications running in� The prototype scope. If the scope is set to prototype, the Spring IoC container creates a new bean instance of the object every time a request for that specific bean is made. As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.


4.4 Bean scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object,� The isPrototypeOf() method allows you to check whether or not an object exists within another object's prototype chain. Examples Using isPrototypeOf. This example demonstrates that Baz.prototype, Bar.prototype, Foo.prototype and Object.prototype exist in the prototype chain for object baz:


Spring, But if the bean A has the narrower scope say prototype scope then there's a problem. To understand the problem let's see an example. We are� In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton. P.S * means only valid in the context of a web-aware Spring ApplicationContext. Singleton vs Prototype. Here’s an example to show you what’s the different between bean scope : singleton and prototype.


spring, A prototype-scoped bean is not pre-created on Spring container startup. Instead, a new fresh instance will be created every time a request to retrieve this bean is� A scope of work document is not a proposal and should never be treated as such. It’s actually a document that’s drawn up, often immediately before you commence working. It’s usually typed up and sent to a company once the sale has been made, and includes a quote for the work that will be done, as well as a timeline for the project.