java - Spring singleton bean 在被代理时在每个方法调用上创建新的实例/bean

标签 java spring proxy prototype scopes

下面是代码。当您运行它并在浏览器中进入 localhost:8080/hello 时,您将看到序列“1,2”、“2,3”...,因为 Spring CGLIB 代理(实际上插入到原型(prototype)字段中)创建了新 bean在每个方法调用上。

我认为原型(prototype) bean 应该在每次 http 调用时只创建一次,因此输出应该是“1,1”、“2,2”..

我可以使用 ObjectFactory 解决这个问题,但随后我会失去代理以及所有 AOP spring 功能。

这应该如何表现?我有错吗?这真的是Spring的限制吗?可以通过基于Java的配置以某种方式解决吗?

这是整个代码,您只需要 2 个文件:

应用程序.java

@SpringBootApplication
@RestController
public class Application {

    @Autowired  //one solution is ObjectFactory<PrototypeExample>
    private PrototypeExample prototype;

    @GetMapping("/hello")
    public String hello() {
        return (prototype.getCounter() + ", " + prototype.getCounter());
    }

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

@Component
@Scope(value = SCOPE_PROTOTYPE, proxyMode = TARGET_CLASS)
class PrototypeExample {

    private static AtomicInteger counter = new AtomicInteger();

    public PrototypeExample() {
        counter.getAndIncrement();
    }

    public int getCounter() {
        return counter.get();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.neco</groupId>
    <artifactId>spring-core_IV</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

最佳答案

您的 bean PrototypeExample 具有 SCOPE_PROTOTYPE 作用域属性。

根据official documentation :

The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made. That is, the bean is injected into another bean or you request it through a getBean() method call on the container. As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans.

如果您需要单例,只需删除@Scope注释或将其设置为singleton(这是默认的)。列出了所有可用范围 here .

关于java - Spring singleton bean 在被代理时在每个方法调用上创建新的实例/bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856924/

相关文章:

java - 关于说明通用类语法用法的示例

java - Google Drive Webhook 通知请求正文为空

Node.js - 通过外部 HTTP 代理进行 OAuth

c# - 动态端口转发 libssh C

node.js - 尝试使用 npm install 时如何解决 NPM "418 I' m a teapot"错误?

java - 构建为 jar 时读取 .shader 文件

java - Java 中的计时器任务问题

java - RecyclerView 列表项目不显示也不播放

java - 使用 Spring 注入(inject) TaskScheduler

spring - 关闭程序的 DefaultMessageListenerContainer 问题