简体   繁体   English

Spring创建原型bean两次?

[英]Spring creating prototype bean twice?

I have a simple spring boot app with the following config:我有一个简单的 Spring Boot 应用程序,配置如下:

@Configuration
public class MyConfig {
    @Bean
    @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public HashMap<String, BidAskPrice> beanyBoi() {
        System.out.println("creating a new one");
        return dataFetcher().getPairPricesBidAsk();
    }
}

and i have a service我有一个服务

@Service
public class ProfitCalculatorService {

  @Autowired
  private HashMap<String, BidAskPrice> prices;

  public HashMap<String, BidAskPrice> getPrices() {
    return prices;
  }
}

and i have a controller我有一个控制器

@RestController
public class TestController {

  @Autowired
  ProfitCalculatorService profitCalculatorService;

  @GetMapping("/getprices")
    public HashMap<String, BidAskPrice> someprices() {
      return profitCalculatorService.getPrices();
    }
  }
}

now when i hit the /getprices endpoint, i was seeing some odd behaviour.现在,当我点击 /getprices 端点时,我看到了一些奇怪的行为。 The following message is logged twice: "creating a new one".以下消息被记录两次:“正在创建一个新的”。

Your beans have a prototype scope, it means that every time that you asked for this bean a new instance will be created.您的 bean 有一个原型作用域,这意味着每次您请求此 bean 时,都会创建一个新实例。

Official explanation:官方解释:

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. bean 部署的非单一原型范围导致每次对特定 bean 发出请求时都会创建一个新 bean 实例。 That is, the bean is injected into another bean or you request it through a getBean() method call on the container.也就是说,bean 被注入到另一个 bean 中,或者您通过容器上的 getBean() 方法调用来请求它。 As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.通常,您应该对所有有状态 bean 使用原型作用域,对无状态 bean 使用单例作用域。

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-scopes-prototype https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-scopes-prototype

Another thing is the proxyMode with TARGET_CLASS value.另一件事是具有TARGET_CLASS值的proxyMode It means that the bean injected into the ProfitCalculatorService is not the BidAskPrice itself, but a proxy to the bean (created using CGLIB) and this proxy understands the scope and returns instances based on the requirements of the scope (in your case prototype).这意味着注入ProfitCalculatorService的 bean 不是BidAskPrice本身,而是 bean 的代理(使用 CGLIB 创建),并且该代理了解范围并根据范围的要求返回实例(在您的情况下是原型)。

So, my suggestion is: You don't need to create an explicitly bean of HashMap<String, BidAskPrice> .所以,我的建议是:您不需要创建HashMap<String, BidAskPrice>的显式 bean。 Since BidAskPrice is a bean, you can inject the HashMap<String, BidAskPrice> directly in your service, Spring will manage this list for you!由于BidAskPrice是一个 bean,你可以直接在你的服务中注入HashMap<String, BidAskPrice> ,Spring 会为你管理这个列表!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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