@Inject无状态EJB包含来自先前请求的数据 [英] @Inject stateless EJB contains data from previous request
问题描述
我有一个包含EJB的业务逻辑的jar文件 server.jar
为了显示我的问题,我创建了一个无状态的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调用的奇怪行为:
@Stateless
public class SubsequentTestService
{
private String value;
public void testIt()
{
System.out.println(###### VALUE2:+ value);
value =TestValue2;
}
}
更改注释表单 @ EJB
到 @Inject
不会更改任何内容。
我的 web.war
我有简单的JAX-RS bean。被称为显示奇怪行为的定义如下:
@Path(/ test)
public class TestResource
{
@Inject
TestService testService;
@GET
@Path(/ state)
public void testState()
{
testService.testIt();
}
}
JAX-RS应用程序配置如下所示: / p>
@ApplicationPath(/ api)
public class JaxRsConfiguration extends Application
{
}
war文件包含beans.xml,但没有其他配置文件。一切都被打包成一个ear文件,并部署在wildfly 10.0.0.Final中。如果我通过 http:// localhost:8080 / api / test / state 我得到预期的输出:
INFO [stdout](默认任务-7)####### VALUE:null < br>
INFO [stdout](默认任务-7)####### VALUE2:null
但是在第二个请求中,我收到以下意外的输出:
INFO [stdout](默认任务-8)####### VALUE :TestValue
INFO [stdout](默认任务-8)####### VALUE2:TestValue2
这里有什么问题?在野鸭中可能会配置错误吗?但是我只是改变了日志和数据源的定义。
你的意思是 @Stateless
向后。
这并不是这样的:
嘿,容器,这是一个任意的类,请使它成为一个无状态的bean。
意思就是这样:
嘿容器,这里是一个无状态的类,你可以安全地使用它作为无状态的bean。 >
你有一个有状态的类。您应该将其标记为 @Stateful
bean。否则,摆脱所有状态(非托管实例变量),以便您可以安全地将其用作 @Stateless
bean。
另见:
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.
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();
}
}
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";
}
}
Changing the annotation form @EJB
to @Inject
does not change anything.
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();
}
}
The JAX-RS application configuration looks as follows:
@ApplicationPath("/api")
public class JaxRsConfiguration extends Application
{
}
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:
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] (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.
You have the meaning of @Stateless
backwards.
This does not mean like so:
Hey container, here's an arbitrary class, please make it a stateless bean.
This actually means like so:
Hey container, here's a stateless class, you can safely use it as a 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.
See also:
- Why Stateless session beans?
- JSF request scoped bean keeps recreating new Stateful session beans on every request?
- When using @EJB, does each managed bean get its own @EJB instance?
这篇关于@Inject无状态EJB包含来自先前请求的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!