简体   繁体   English

注入 singleton bean 时,Spring 的 @RequestScope 会自动处理代理吗?

[英]Does Spring's @RequestScope automatically handle proxying when injected in singleton beans?

I'm using a Java8/Spring Boot 2 application.我正在使用 Java8/Spring Boot 2 应用程序。 I want to inject a request-scoped bean into a singleton bean.我想将一个请求范围的 bean 注入到一个 singleton bean 中。 The official documentation highlights that either a proxy or ObjectFactory/Provider should be used to ensure always getting the correctly scoped bean at runtime in the singleton bean. 官方文档强调应使用代理或 ObjectFactory/Provider 来确保在运行时始终在 singleton bean 中获得正确作用域的 bean。 However, the @RequestScope annotation seems to "automatically" set some kind of proxy, as explained in the answer to this question .但是,@RequestScope 注释似乎“自动”设置了某种代理,如该问题的答案中所述。

I'm now wondering if the following three implementations are in fact identical and which one is preferred?我现在想知道以下三个实现是否实际上相同,哪个是首选?

Approach 1: explicitly using objectFactory<>方法 1:显式使用 objectFactory<>

@Component
@RequestScope
public class MyRequestScopedBean{...}

@Component
public class MySingletonBean{
    @Autowired
    private ObjectFactory<MyRequestScopedBean> myRequestScopedBean
}

Approach 2: inject normally, assuming the request scoped bean is proxied automatically?方法 2:正常注入,假设请求作用域 bean 被自动代理?

@Component
@RequestScope
public class MyRequestScopedBean{...}

@Component
public class MySingletonBean{
    @Autowired
    private MyRequestScopedBean myRequestScopedBean
}

Approach 3: using @Configuration and @Bean because I don't know the difference and I'm worried they behave differently.方法 3:使用 @Configuration 和 @Bean 因为我不知道它们的区别并且我担心它们的行为不同。

@Comfiguration
public class myBeanConfig{
   @Bean
   @RequestScope
   public MyRequestScopedBean getRequestScopedBean(){return new MyRequestScopedBean();}

}

@Component
public class MySingletonBean{
    @Autowired
    private MyRequestScopedBean myRequestScopedBean
}

I would prefer approach 2, because it is concise and handles the scoping/proxying automatically.我更喜欢方法 2,因为它简洁并自动处理范围/代理。

Would the answer change if my @Autowired bean is declared as a final field?如果我的 @Autowired bean 被声明为final字段,答案会改变吗? I'm worried making it final somehow prevents the proxy from fetching the correctly fetching the new bean every request.我担心将其设置为 final 会以某种方式阻止代理在每个请求中正确获取新 bean。

Yes, with @RequestScope the proxy is already default activated, the effect exactly equal to @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyModel = ScopedProxyMode.TARGET_CLASS)是的,使用@RequestScope代理已经默认激活,效果完全等于@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyModel = ScopedProxyMode.TARGET_CLASS)

I have been using the 2nd approach in my projects and I have zero issues so far. 我一直在我的项目中使用第二种方法,到目前为止我没有问题。 The documentation does not mention it's a MUST to use ObjectFactory too. 文档中没有提到它也必须使用ObjectFactory Don't think too much. 不要想太多。 If you run into any problems, you will see the error very clearly in the console. 如果遇到任何问题,您将在控制台中非常清楚地看到错误。 There's no reason to be afraid until you have an actual issue to deal with. 在你遇到实际问题之前没有理由害怕。

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

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