Java 使用@Scope将单例更改为原型(“原型”)

Java 使用@Scope将单例更改为原型(“原型”),java,spring,spring-boot,Java,Spring,Spring Boot,我希望/dog2是不同的对象,但我仍然不知道@Scope(“prorotype”)为什么不适合我。我尝试使用另一个作用域,但仍然存在相同的问题-我转到/dog,然后转到/dog2,在两个“Sharo”上看到的是/dog2上的“null” SpringProjectApplication.java package com.example.demo.springproject; import org.springframework.boot.SpringApplication; import or

我希望/dog2是不同的对象,但我仍然不知道@Scope(“prorotype”)为什么不适合我。我尝试使用另一个作用域,但仍然存在相同的问题-我转到/dog,然后转到/dog2,在两个“Sharo”上看到的是/dog2上的“null”

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();
}

}
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();
}
}
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 AnimalController {

@Autowired
private Animal dog;

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

}
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();
}
}
编辑: Dog.java


看起来您有语法错误。 试一试

@Scope(“原型”)


如果这不能解决您的问题,请共享您的动物和狗类。

SpringMVC中的控制器是单例的。在多个请求之间共享类变量。您可以在控制器类上方尝试@Scope(“request”)注释。

您正在为
狗使用
@Bean
@Component
注释,此时您应该选择其中一种。要么删除
@Bean
并将
@Scope
添加到
Dog
类中,要么从类中删除
@组件
注释。

您真的编写了
prorotype
吗?是的,完全相同,为什么?哦,我看到了。。。我只是改变了太多的方式,最后我想我打错了。。。这不是问题。你看到什么不对劲了吗?
Dog
类是什么样子的?非常感谢。我只是想请你再做一个评论,这样我就可以标记它了。救了我一天!没问题,很高兴我能帮忙。