|
|
|
|
Blog信息
|
blog名称:四裤全输的小窝~~ 日志总数:178 评论数量:699 留言数量:198 访问次数:1152417 建立时间:2005年10月29日 | |
|
|
|
|
 |
|
|
|
|
[Hibernate]weblogic中将SessionFactory配置为JNDI
文章收藏, 网上资源, 软件技术, 电脑与网络 四裤全输 发表于 2006/7/5 10:44:46
|
转自:http://searchwebservices.techtarget.com.cn/tips/315/2175315.shtml为了更方便的在编写程序的过程中运用hibernate并且在多个应用程序中共享SessionFactory,将生成的SessionFactory配置为jndi并且在服务器启动的时候将其配置为自动运行是个不错的方法,这样在程序中就可以通过“查找”的方式直接获取factory的引用,步骤如下: 1. 如果单单是为了将SessionFactory配置为JNDI,方法很简单,只要在hibernate.cfg.xml文件中配置一下要绑定到jndi的名字即可,格式如下:<hibernate-configuration> <session-factory name="hibernate.session_factory"> <property name="connection.datasource">jdbc.name</property> …………………… /session-factory> </hibernate-configuration> 其中name="hibernate.session_factory"指定要绑定的JNDI名字为 hibernate/session_factory,这样在hibernate初始化SessionFactory的时候就会将其绑定在jndi服务上,之后我们就可以在程序中通过JNDI查找来获取SessionFactory的引用,如: Context context=new InitialContext(); SessionFactory sf = (SessionFactory)context.lookup("hibernate/session_factory"); 2. 上述中通过JNDI查找SessionFactory的引用的前提是在查找之前必须已经绑定,也就是hibernate已经初始化过 SessionFactory,为了保证此条件,比较好的方法就是在服务器启动的时候自动运行初始化SessionFactory的代码并将其绑定至 JNDI,可以通过以下两种方式实现服务器启动的时候自动运行代码: 对于weblogic来说,我们可以通过weblogic.common.T3StartupDef接口来实现一个StartUp类,然后在weblogic控制台中将其配置为自动运行,步骤如下: 1) 编写StartUp类: package com.starup public class HibernateInitStartUp implements weblogic.common.T3StartupDef{ public void setServices(T3ServicesDef t3ServicesDef) { } public String startup(String string, Hashtable hashtable) throws Exception { Configuration conf = new Configuration().configure(); SessionFactory sf = conf.buildSessionFactory(); return "SessionFactory init complete and bound to JNDI successful"; } } 2) 编译该类 3) 修改startWebLogic.cmd文件,将上述类中用到的类文件和hibernate关联的包增加到其启动时的classpath环境变量中,在加入的时候要根据hibernate的版本而定,例如我用的是2.0,如果其他的版本上述的文件名可能有些不同,此时一定要检查好 4) 打开weblogic控制台,在左侧的树中点击StartUp & Shutdown,然后在右边的主页面中点击“Configure a new Startup Class...”配置一个自动运行类,在Name框里面随便填写,在ClassName里面填写你编写的StartUp类:com.starup. HibernateInitStartUp,然后点击apply,在接下来的Target选项中选择myserver(根据你的情况而定),点击 apply按钮。 5) 重新启动weblogic,可以看到hibernate初始化的一些信息和提示将SessionFactory绑定至JNDI的提示信息 6) 在weblogic控制台中的jndi树中可以看到绑定的情况(点击mydomain—server—myserver,然后点击右边页面最下方的“View JNDI tree”就可以看到其信息) 通过实现ServletContextListener接口在系统初始化一个应用程序的时候自动运行,步骤如下: 1) 编写类,实现ServletContextListener接口,如下: package com.starup; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class HibernateInitStartUp implements ServletContextListener { public void contextDestroyed(ServletContextEvent servletContextEvent) { } public void contextInitialized(ServletContextEvent servletContextEvent) { try { Configuration conf = new Configuration().configure(); SessionFactory sf = conf.buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); } } } 2) 在对应应用程序的web.xml文件中加入: com.starup. HibernateInitStartUp 3) 启动wls,系统会在初始化对应应用程序的时候自动运行。 此方法不用配置weblogic的启动环境变量,因为当初始化应用程序的时候,对应的类和包已经加载到环境变量中。
|
阅读全文(2453) | 回复(0) | 编辑 | 精华 |
|
|
|
|
|
|
|