本文介绍了使用特殊的自动启动Servlet在启动时初始化并共享应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要进行一些配置并将其连接到某个地方的外部资源/对象/系统,并将其存储在应用程序范围内。I need to get some configuration and connect to external resources/objects/systems somewhere and store it in application scope.我可以看到两种设置应用程序的方法:I can see two ways to setup my application: 覆盖现有servlet中的 init()以及此处所需的代码, 具有某种初始化servlet,并使用其 init()来完成工作。然后将创建的对象存储在 ServletContext 中,以便与其他servlet共享。Overriding the init() in the existing servlets and required code there and keeping all constructed objects inside that same servlet.Having some kind of an initialisation servlet and using its init() to do the work. Then storing created objects in ServletContext to share it with my other servlets.以上哪种方法更好?有没有更好的方法在servlet之间共享对象?直接从彼此之间调用它们左右吗??Which out of above is better approach? Is there any better way to share objects between servlets? Calling them directly from one another or so...?推荐答案两者都不是更好的方法。 Servlet用于侦听HTTP事件(HTTP请求),而不侦听部署事件(启动/关闭)。None of both is the better approach. Servlets are intended to listen on HTTP events (HTTP requests), not on deployment events (startup/shutdown).@WebListenerpublic class Config implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { // Do stuff during webapp's startup. } public void contextDestroyed(ServletContextEvent event) { // Do stuff during webapp's shutdown. }}如果您不在Servlet上3.0尚未升级,因此无法使用 @WebListener 注释,那么您需要在 / WEB-INF /中手动注册它web.xml 如下:If you're not on Servlet 3.0 yet and can't upgrade, and thus can't use @WebListener annotation, then you need to manually register it in /WEB-INF/web.xml like below:<listener> <listener-class>com.example.Config</listener-class></listener>要在应用程序范围内存储和获取对象(以便所有servlet都可以访问它们),请使用 ServletContext#setAttribute() 和 #getAttribute() 。To store and obtain objects in the application scope (so that all servlets can access them), use ServletContext#setAttribute() and #getAttribute().下面是一个示例,它允许侦听器将自身存储在应用程序范围内:Here's an example which lets the listener store itself in the application scope: public void contextInitialized(ServletContextEvent event) { event.getServletContext().setAttribute("config", this); // ... },然后在servlet中获取它:and then obtain it in a servlet: protected void doGet(HttpServletRequest request, HttpServletResponse response) { Config config = (Config) getServletContext().getAttribute("config"); // ... }在JSP EL中也可用 $ {config} 。因此,您也可以使其成为一个简单的bean。It's also available in JSP EL by ${config}. So you could make it a simple bean as well.import javax.enterprise.context.ApplicationScoped;@ApplicationScopedpublic class Config { public void init(@Observes @Initialized(ApplicationScoped.class) ServletContext context) { // Do stuff during webapp's startup. } public void destroy(@Observes @Destroyed(ApplicationScoped.class) ServletContext context) { // Do stuff during webapp's shutdown. }}可通过 @Inject 。如有必要,还可以 @Named 进行设置,因此也可以通过EL中的#{config} 来使用。This is available in a servlet via @Inject. Make it if necessary also @Named so it's available via #{config} in EL as well.应注意,这是自CDI 1.1以来的新增功能。如果您仍使用CDI 1.0并且无法升级,请选择另一种方法。Noted should be that this is new since CDI 1.1. If you're still on CDI 1.0 and can't upgrade, then pick another approach.import javax.faces.bean.ManagedBeanimport javax.faces.bean.ApplicationScoped;@ManagedBean(eager=true)@ApplicationScopedpublic class Config { @PostConstruct public void init() { // Do stuff during webapp's startup. } @PreDestroy public void destroy() { // Do stuff during webapp's shutdown. }}可通过#获得在EL中也是{config} 。@Startup@Singletonpublic class Config { @PostConstruct public void init() { // Do stuff during webapp's startup. } @PreDestroy public void destroy() { // Do stuff during webapp's shutdown. }}可通过 @EJB 。我说的是考虑,因为您不应为了启动挂钩而滥用EJB。此外,默认情况下, @Singleton 处于读/写锁定状态,主要用于事务性事务,例如安排后台作业。This is available in a servlet via @EJB. I'm saying "consider", because you should not abuse an EJB for the sake of a startup hook. Moreover, a @Singleton is by default read/write locked and primarily intented for transactional stuff such as scheduling background jobs. 如何在基于servlet的Web应用程序中运行后台任务? ServletContainerInitializer与ServletContextListenerHow to run a background task in a servlet based web application?ServletContainerInitializer vs ServletContextListenerHow do I force an application-scoped bean to instantiate at application startup? 这篇关于使用特殊的自动启动Servlet在启动时初始化并共享应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!