简体   繁体   English

当我将无状态Bean注入有状态Bean时会发生什么?

[英]What happens when I inject a Stateless Bean into a Stateful Bean?

I work on a large monolithic Java web system, built with JSF 1.1 and Seam Framework, running on a jBoss Server. 我在一个大型的整体Java Web系统上工作,该系统由jSF 1.1和Seam Framework构建,并在jBoss服务器上运行。

I have a JSF Backing Bean (Stateful by default in JSF) that injects a Stateless Bean used as a service (and annoted with @Stateless). 我有一个JSF Backing Bean(在JSF中默认为有状态),它注入用作服务的无状态Bean(并带有@Stateless注释)。 The Stateless Service has a instance member variable declared as a List and increased by a method. 无状态服务具有一个实例成员变量,该成员变量声明为List并通过方法增加。

Issue: After updating jBoss version from 5.1 EAP to 7.0, the mentioned variable (list) is taking values from other requests. 问题:将jBoss版本从5.1 EAP更新到7.0后,上述变量(列表)正在从其他请求中获取值。 This creates a bug in the system. 这会在系统中创建错误。 But, the variable is inside a stateless service. 但是,该变量位于无状态服务内部。 Why is this happening? 为什么会这样呢? What should be happening in this situation? 在这种情况下应该发生什么?

ps: I have so many things that can be used to solve the issue, but I need to understand what is happening. ps:我有很多东西可以用来解决问题,但是我需要了解正在发生的事情。

Edit: Pproblem solved, i was working with jBoss 5 and Seam Framework by several years. 编辑:Pproblem解决了,我使用jBoss 5和Seam Framework已有几年了。 I was confused by a wrong jBoss 5 comportament. 一个错误的jBoss 5组合使我感到困惑。 After read the definition to a Steteless Bean, i understand very well what are hepenning with my code: [Java EE 1.4 Tutorial 1 在阅读了Steteless Bean的定义之后,我非常了解我的代码在处理什么:[Java EE 1.4教程1

After update to jBoss 7, the pool of stateless beans work as expected and the system code gives a bug because a bad implementation of my team (stateless beans with so many global variables). 更新到jBoss 7之后,无状态Bean的池将按预期工作,并且系统代码会出现错误,因为我的团队执行不正确(具有很多全局变量的无状态Bean)。 When tho jBoss pool started works, the values started to be shared with others requests. 当jBoss池开始工作时,这些值开始与其他请求共享。

This is because your stateless instance is shared among several stateful instances. 这是因为您的无状态实例在多个有状态实例之间共享。

Member variables of stateless beans almost behave as global variables at runtime. 无状态Bean的成员变量在运行时几乎充当全局变量。 So : - if your list depends on a state, put it in a stateful bean - otherwise keep it as is but with a purge mechanism - the third way to be "stateful in stateless" but only in web servers consists in using ThreadLocal variables 因此:-如果您的列表依赖于状态,则将其放入有状态的Bean-否则将其保留,但要使用清除机制-第三种在“无状态”中“有状态”的方式(仅在Web服务器中)是使用ThreadLocal变量

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

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