構成ファイルを使用せずに、アプリに組み込みの tomcat インスタンスを構成しようとしています。
私はいくつかの調査を行い、この長いチュートリアルと短いチュートリアルに基づいて、この手順を抽出しました。
作成する
ServletContextListener
@WebListener //some articles on the web mentioned, that this would add the //Listener automatically to the app context, but i cant believe that this works in my case public class HibernateListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { HibernateUtil.getSessionFactory(); // create a factory } public void contextDestroyed(ServletContextEvent event) { HibernateUtil.getSessionFactory().close(); // free resources } }
そのリスナーをアプリ コンテキストに追加します。
Context rootCtx = tomcat.addContext("", base.getAbsolutePath()); rootCtx.getServletContext().addListener("com.example.listeners.HibernateListener"); tomcat.start(); tomcat.getServer().await();
HibernateUtil
必要な構成でクラスを実装するpublic class HibernateUtil { private static final SessionFactory sessionFactory; static { try { //should i call .configure() on the returned Configuration here? sessionFactory = getConfiguration() .buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } private static Configuration getConfiguration(){ Configuration c = new Configuration(); c.setProperty("hibernate.connection.url", "jdbc:hsqldb:hsql://localhost:1234/mydb1"); c.setProperty("hibernate.connection.username", "SA"); c.setProperty("hibernate.connection.password", ""); c.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver"); c.setProperty("dialect", "org.hibernate.dialect.HSQLDialect"); c.setProperty("cache.provider_class", "org.hibernate.cache.NoCacheProvider"); c.setProperty("cache.use_query_cache", "false"); c.setProperty("cache.use_minimal_puts", "false"); c.setProperty("max_fetch_depth", "3"); c.setProperty("show_sql", "true"); c.setProperty("format_sql", "true"); c.setProperty("hbm2ddl.auto", "create"); c.addPackage("com.example.models"); c.addAnnotatedClass(MyClass.class); return c; } public static SessionFactory getSessionFactory() { return sessionFactory; } }
今、どうにかし
MyClass
て、休止状態を介してリンクされたデータベースからデータを作成および取得する必要がありますよね? (今のところ、正確にはわかりませんが、それはここでのポイントではありません)
しかし、残念ながらNullPointerException
、リスナーをTomcatに追加しようとすると、エラーが発生します
org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1278) でのスレッド「メイン」java.lang.NullPointerException org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:649) での例外
ラインを指すrootCtx.getServletContext().addListener("com.example.listeners.HibernateListener");
編集1
しかし、休止状態のスタンドアロン(Tomcat なし) を実行している場合は、正常に動作します。データはエラーなしで保存されています!
のHibernateUtil
public static void main(String[] args) {
MyClass mycls = new MyClass();
mycls.setMyProperty("My Property");
Session session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
session.save(mycls);
transaction.commit();
}
したがって、休止状態を構成する方法は問題ないと思います。エラーは、リスナーの追加に関係しています...
ここで何が間違っていますか?