@PostConstruct 方法在 Jersey 服务上一直被调用

发布于 2024-10-04 01:05:32 字数 776 浏览 1 评论 0原文

您好,我正在使用 GigaSpaces XAP,它基本上使用 Spring 和 ApplicationContext 来完成所有初始化和配置等...在加载 Web 应用程序期间的某个时间点,会实例化“缓存”或他们所谓的空间代理,并且通过 ServletContext 可用。该空间代理允许您对集群缓存进行写入和读取。

所以我最初所做的是获取每个 REST 方法的空间。所以...

@GET
public String myMethod()
{
space = (GigaSpace)context.getAttribute("mySpace");
space.write(new HelloWorld());
space.read(....);
etc...
return "Hello World!";
}

由于空间本身是线程安全的,GigaSpaces 人员指示我在“应用程序”初始化时查找空间一次,这样我就可以节省空间查找的时间。

所以我查看了我所做的@PostConstruct...

@PostConstruct
public void init()
{
space = (GigaSpace)context.getAttribute("mySpace");
}

但似乎我提出的每个请求都会调用这个方法!这是否意味着我的 REST 服务正在为我发出的每个请求创建?是因为我使用的是@Scope("request")吗?

如果有帮助,servlet 容器是 Jetty 7.1.4,并且我正在使用标准 WAR 进行部署。

Hi I'm using GigaSpaces XAP which basically uses Spring and ApplicationContext to do all it's init and config etc... At some point in time during the loading of the web application a "cache" or what they call a space proxy is instantiated and is made available through the ServletContext. This space proxy is what allows you to write and read to and from the clustered cache.

So what I did initially was to get the space on each REST method. So...

@GET
public String myMethod()
{
space = (GigaSpace)context.getAttribute("mySpace");
space.write(new HelloWorld());
space.read(....);
etc...
return "Hello World!";
}

Since the space itself is thread safe I was instructed by the GigaSpaces guys to lookup the space once on init of my "application" so i can save on the lookup of the space.

So I looked into @PostConstruct where I did...

@PostConstruct
public void init()
{
space = (GigaSpace)context.getAttribute("mySpace");
}

But it seems that this method is being called on every request I make! Does this mean my REST service is being created for each request I make? Is it because I'm using @Scope("request")?

If it Helps the servlet container is Jetty 7.1.4 and I'm using standard WAR to deploy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-10-11 01:05:32

但当然! System.out.println("是你的朋友!")

是的,这是因为 @Scope("request") 更改为 @Scope("singleton") 导致球衣“bean”实例化一次而不是每个请求实例化。

But of course! System.out.println("Is your friend!")

And yes it is because of @Scope("request") changing to @Scope("singleton") causes the jersey "bean" to instantiate once instead of per request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文