Stripes 中的实例变量
我正在尝试找到一种在 Stripes 应用程序上下文中创建实例变量的方法。 在使用手工编码的 Servlet 时,我会在 Servlet 的 init()
方法中执行一些操作。 问题在于,由于每次访问应用程序时都会创建 ActionBean 的实例,因此会多次创建 actionBean 中的变量。 我试图通过 Stripes 尝试通过 ActionBeanContext.getServletContext() 调用 ServletContext 找到一些合理的位置,但从那里无法访问 init () 方法并在其中编写一些代码。
您有什么建议吗?
I'm trying to find a way to create an instance variable within the Stripes application context.
Something that i would do in the init()
method of a Servlet while using hand-coded servlets.
The problem is that since an instance of the ActionBean
is created each time the application is accessed, the variable in the actionBean is created multiple time.
I have tried to get some reasonable place withing Stripes trying to call the ServletContext
via ActionBeanContext.getServletContext()
, but from there there is no way to access the init()
method and write some code in it.
Do you have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ActionBeanContext 也是 Stripes 应用程序上下文。该上下文可以自定义,并且可以包含您想要的任何内容。一些示例代码:
让 Stripes 上下文工厂 知道您想要的要使用自定义 ActionBeanContext,您需要将 init-param 添加到 web.xml 中的 Stripes 过滤器:
您可以通过添加 SerlvetContextListener 在服务器启动时初始化对象:
ActionBean 示例:
另一种解决方案,在我看来更好的解决方案是使用 依赖注入 框架来注入 (singleton) 对象到你的actionbeans中。请参阅此处的 Stripes 配置示例:使用 Guice DI 注入 Stripes ActionBeans
The ActionBeanContext is also Stripes application context. This context can be customized and can contain whatever you want. Some example code:
To let the Stripes context factory know you want to use a custom ActionBeanContext you need to add an init-param to the Stripes filter in the web.xml:
You can initialize your object at server start by adding a SerlvetContextListener:
Example ActionBean:
An alternative solution, in my opinion a superiour solution, is to use a dependency injection framework for injecting the (singleton) objects into your actionbeans. See Stripes configuration example here: Injecting Stripes ActionBeans with Guice DI
不是 Stripes 特定的方式,而是使用标准 Servlet API,您可以实现
ServletContextListener
并在contextInitialized()
方法中完成这项工作。如果您在web.xml
中将其注册为
(或者当您已经使用 Java EE 6 时,请使用@WebListener
进行注释) ),然后它将在 webapp 启动期间运行。这样,它可以通过
${somename}
在 EL 中使用,并通过ServletContext#getAttribute()
在所有操作 bean 中使用。Not a Stripes-specific way, but using the standard Servlet API you'd implement
ServletContextListener
and do the job incontextInitialized()
method. If you register it as<listener>
inweb.xml
(or when you're already on Java EE 6, annotate using@WebListener
), then it'll run during webapp's startup.This way it's available in EL by
${somename}
and in all action beans byServletContext#getAttribute()
.@JBoy,您必须在 web.xml 中指定 ServletContextListner 的实现,如下所示,
感谢 KDeveloper 的建议。我也在寻找解决方案。我从他的博客上找到了信息
我发现了另一种方法。为此,您必须对“RuntimeConfiguration”类进行子类化
,然后在 web.xml 中指定上述配置。
正如 KDeveloper 所说,您还必须对 ActionBeanContext 进行子类化;获取 ActionBeans 中的对象
这是我的发现。我发现它正在发挥作用。但不知道有没有副作用。如果有的话;请评论..
@JBoy, You have to specify your implementation of ServletContextListner in the web.xml like below
Thanks KDeveloper for his advice. I was also searching for the solution. I found the information from his blog
There is one more method I have found out. For that you have to subclass the "RuntimeConfiguration" class
After that in the web.xml specify the above configuration.
You also have to subclass the ActionBeanContext as KDeveloper said; to get the object in ActionBeans
This is my finding. I found out it is working. But I don't know whether it has any side effects. If it has any; please comment..