简体   繁体   中英

Changing from Singleton to Prototype with @Scope(“prototype”)

I want /dog2 to be different object but i still have no idea why @Scope("prorotype") is not working for me. I tried with another Scopes but still there is the same problem - i go to /dog then to /dog2 and i see on both "Sharo" instead to see "null" on /dog2

SpringProjectApplication.java

package com.example.demo.springproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;

import com.example.demo.springproject.entities.Animal;
import com.example.demo.springproject.entities.Dog;

@SpringBootApplication
public class SpringprojectApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringprojectApplication.class, args);
    }

    @Scope("prototype")
    @Bean
    public Animal getDog() {
        return new Dog();
    }
}

AnimalController.java

package com.example.demo.springproject.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.springproject.entities.Animal;

@Controller
public class AnimalController {

@Autowired
private Animal dog;

@GetMapping("/dog2")
@ResponseBody
public String getDog() {
    if (dog.getName() == null) {
        return "null";
    }
    return dog.getName();
}

}

DogController.java

package com.example.demo.springproject.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.springproject.entities.Animal;

@Controller
public class DogController {

@Autowired
private Animal dog;

@GetMapping("/dog")
@ResponseBody
public String getHomePage() {
    dog.setName("Sharo");
    return dog.getName();
}
}

EDIT: Dog.java

package com.example.demo.springproject.entities;

import org.springframework.stereotype.Component;

@Component
public class Dog implements Animal {

private String name;

public Dog() {

}

@Override
public String getName() {
    return name;
}

@Override
public void setName(String name) {
    this.name = name;
}

}

You're using both @Bean and @Component annotations for Dog , when you should be choosing one or the other. Either remove the @Bean and add @Scope to Dog class, or remove the @Component annotation from the class.

Looks like you have a syntax error. try

@Scope("prototype") instead

If this doesn't resolve your issue, please share your animal and dog classes.

Controllers in SpringMVC are singletons. Between multiple requests your class variable get shared. You can try @Scope("request") annotation above your controller class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM