java 在 Jersey webapp 启动时初始化数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12875682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize database on Jersey webapp startup
提问by Sotirios Delimanolis
I've read thisbut I don't quite understand how it works. I want to load a properties file and set up my connection pool when my web application starts. Obviously I want to do this only once and in a single place so I can change it if needs be. With regular servlets, I would simply put my initialization code in the servlet's init() method, but you don't have access to it with a Jersey servlet. So where do I do it? How do the listeners in the link above work?
我读过这个,但我不太明白它是如何工作的。我想在 Web 应用程序启动时加载一个属性文件并设置我的连接池。显然,我只想在一个地方做一次,这样我就可以在需要时进行更改。对于常规 servlet,我只需将初始化代码放在 servlet 的 init() 方法中,但您无法使用 Jersey servlet 访问它。那么我在哪里做呢?上面链接中的听众如何工作?
回答by Mario Dennis
All you need to do is write a java class that implements the ServletContextListener interface. This class must implement two method contextInitialized method which is called when the web application is first created and contextDestroyed which will get called when it is destroyed. The resource that you want to be initialized would be instantiated in the contextInitialized method and the resource freed up in the contextDestroyed class. The application must be configured to call this class when it is deployed which is done in the web.xml descriptor file.
您需要做的就是编写一个实现 ServletContextListener 接口的 java 类。此类必须实现两个方法 contextInitialized 方法,该方法在首次创建 Web 应用程序时调用,而 contextDestroyed 方法将在销毁时调用。您要初始化的资源将在 contextInitialized 方法中实例化,并在 contextDestroyed 类中释放资源。应用程序必须配置为在部署时调用此类,这在 web.xml 描述符文件中完成。
public class ServletContextClass implements ServletContextListener
{
public static Connection con;
public void contextInitialized(ServletContextEvent arg0)
{
con.getInstance ();
}//end contextInitialized method
public void contextDestroyed(ServletContextEvent arg0)
{
con.close ();
}//end constextDestroyed method
}
The web.xml configuration
web.xml 配置
<listener>
<listener-class>com.nameofpackage.ServletContextClass</listener-class>
</listener>
This now will let the application call the ServletContextClass when the application is deployed and instantiate the Connection or any other resource place in the contextInitialized method some what similar to what the Servlet init method does.
现在,这将让应用程序在部署应用程序时调用 ServletContextClass,并在 contextInitialized 方法中实例化 Connection 或任何其他资源位置,这与 Servlet init 方法所做的有些类似。
回答by Pace
Since you don't need to modify Jersey itself at startup time you probably don't want an AbstractResourceModelListener. What you want is a javax.servlet.ServletContextListener. You can add listenerelements to your web.xml in the same way you add servlet elements. The ServletContextListener will get called when your context (web application) first gets created and before the Jersey servlet is started. You can do whatever you need to the database in this listener and it will be ready when you start using Jersey.
由于您不需要在启动时修改 Jersey 本身,您可能不需要 AbstractResourceModelListener。你想要的是一个javax.servlet.ServletContextListener。您可以采用与添加 servlet 元素相同的方式将侦听器元素添加到 web.xml 中。ServletContextListener 将在您的上下文(Web 应用程序)首次创建时和 Jersey servlet 启动之前被调用。你可以在这个监听器中对数据库做任何你需要的事情,当你开始使用 Jersey 时它就准备好了。