@Inject 无状态 EJB 包含来自先前请求的数据 [英] @Inject stateless EJB contains data from previous request
问题描述
我有一个 JAX-RS 网络服务,其中包含用于生成测试数据的资源.在测试期间,我发现注入的 EJB 没有重新初始化并且仍然包含来自最后一个请求的数据.
I have a JAX-RS webservice with a resource for generating testdata. During tests I found out, that the injected EJB is not reinitialized and still contains data from the last request.
我有一个 jar 文件 server.jar
,其中包含我的 EJB 业务逻辑.为了显示我的问题,我创建了一个无状态 bean:
I have a jar file server.jar
containing my business logic with EJBs. To show my problem I have created a stateless bean:
@Stateless
public class TestService
{
@EJB
SubsequentTestService state2Service;
private String value;
public void testIt()
{
System.out.println("####### VALUE: " + value);
value = "TestValue";
state2Service.testIt();
}
}
我正在使用对 SubsequentTestService
的后续调用来显示对于另一个无状态 EJB 的调用也存在奇怪的行为:
I am using the subsequent call to SubsequentTestService
to show the odd behaviour also exists for call of another stateless EJB:
@Stateless
public class SubsequentTestService
{
private String value;
public void testIt()
{
System.out.println("####### VALUE2: " + value);
value = "TestValue2";
}
}
将注释形式 @EJB
更改为 @Inject
不会改变任何内容.
Changing the annotation form @EJB
to @Inject
does not change anything.
在我的 web.war
中,我有简单的 JAX-RS bean.被调用以显示奇怪行为的定义如下:
In my web.war
I have simple JAX-RS beans. The one which is called to show the strange behaviour is defined as follows:
@Path("/test")
public class TestResource
{
@Inject
TestService testService;
@GET
@Path("/state")
public void testState()
{
testService.testIt();
}
}
JAX-RS 应用程序配置如下所示:
The JAX-RS application configuration looks as follows:
@ApplicationPath("/api")
public class JaxRsConfiguration extends Application
{
}
war 文件包含 beans.xml,但没有其他配置文件.一切都打包成ear文件,部署在wildfly 10.0.0.Final中.如果我通过 http://localhost:8080/api/test/state 我得到了预期的输出:
The war file contains the beans.xml, but no other configuration file. Everything is packaged into an ear file and is deployed in wildfly 10.0.0.Final. If I call the webservice as GET request via http://localhost:8080/api/test/state I get the expected output:
信息 [标准输出](默认任务 7)####### 值:空
信息 [stdout](默认任务 7)####### VALUE2:空
INFO [stdout] (default task-7) ####### VALUE: null
INFO [stdout] (default task-7) ####### VALUE2: null
但是在第二个请求中,我得到以下意外输出:
But on the second request I get following unexpected output:
INFO [stdout](默认任务 8)####### VALUE:TestValue
信息 [stdout](默认任务 8)####### VALUE2:TestValue2
INFO [stdout] (default task-8) ####### VALUE: TestValue
INFO [stdout] (default task-8) ####### VALUE2: TestValue2
我的问题是什么?野蝇中可能有什么配置错误?但我只更改了日志记录和数据源定义.
What is my problem here? Might be anything misconfigured in the wildfly? But I have only changed the logging and the datasource definition.
推荐答案
你把@Stateless
的意思倒过来了.
You have the meaning of @Stateless
backwards.
这并不意味着像这样:
嘿容器,这是一个任意的类,请将其设为无状态 bean.
这实际上是这样的:
嘿容器,这是一个无状态类,你可以安全地将它用作无状态 bean.
你有一个有状态的类.您应该将其标记为 @Stateful
bean.否则,摆脱所有状态(非托管实例变量),以便您可以安全地将其用作 @Stateless
bean.
You have a stateful class. You should mark it as a @Stateful
bean. Otherwise, get rid of all the state (unmanaged instance variables) so you can safely use it as a @Stateless
bean.
这篇关于@Inject 无状态 EJB 包含来自先前请求的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!