网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.垃圾自动收集系统指导 (1).
.对Java多态性综合运用的探讨.
.如何将form保存到图片中!.
.Netscape服务器端编程技术.
.状态模式之星际应用.
.Taglib原理和实现:再论El和JSTL.
.Java咖啡馆(3)——Eclipse.
.wtp-all-in-one-0.7-win32.
.JAVA数据对象上机实践.
.Java ME中一个通用的游戏信息框架.
.使用Java Servlet动态生成图片.
.在Applet中实现事件向应.
.Struts开发指南之MVC架构.
.关于快速简便的使用AJAX技术操作.
.Sun谈新一代手机前景 充分利用Ja.
.Java序列化的自定义类加载器.
.JAVA加载类库的顺序.
.用JAXB生成一个XML文档.
.改进JAVA字符串分解的方法.
.Java应用的动态扩展.

J2SE综合——关于private构造函数

发表日期:2008-1-5



  看下面的类:
  HibernateSessionFactory.Java
  package zy.pro.wd.util;
  
  import net.sf.hibernate.HibernateException;
  import net.sf.hibernate.Session;
  import net.sf.hibernate.cfg.Configuration;
  
  /**
  * Configures and provides Access to Hibernate sessions, tied to the
  * current thread of execution. Follows the Thread Local Session
  * pattern, see {@link http://hibernate.org/42.Html}.
  */
  public class HibernateSessionFactory {
  
  /**
  * Location of hibernate.cfg.XML file.
  * NOTICE: Location should be on the classpath as Hibernate uses
  * #resourceAsStream style lookup for its configuration file. That
  * is place the config file in a Java package - the default location
  * is the default Java package.
  
  * Examples:
  
  * CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
  * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".
  */
  private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
  
  /** Holds a single instance of Session */
  private static final ThreadLocal threadLocal = new ThreadLocal();
  
  /** The single instance of hibernate configuration */
  private static final Configuration cfg = new Configuration();
  
  /** The single instance of hibernate SessionFactory */
  private static net.sf.hibernate.SessionFactory sessionFactory;
  
  /**
  * Returns the ThreadLocal Session instance. Lazy initialize
  * the SessionFactory if needed.
  *
  * @return Session
  * @throws HibernateException
  */
  public static Session currentSession() throws HibernateException {
  Session session = (Session) threadLocal.get();
  
  if (session == null) {
  if (sessionFactory == null) {
  try {
  cfg.configure(CONFIG_FILE_LOCATION);
  sessionFactory = cfg.buildSessionFactory();
  }
  catch (Exception e) {
  System.err.println("%%%% Error Creating SessionFactory %%%%");
  e.printStackTrace();
  }
  }
  session = sessionFactory.openSession();
  threadLocal.set(session);
  }
  
  return session;
  }
  
  /**
  * Close the single hibernate session instance.
  *
  * @throws HibernateException
  */
  public static void closeSession() throws HibernateException {
  Session session = (Session) threadLocal.get();
  threadLocal.set(null);
  
  if (session != null) {
  session.close();
  }
  }
  
  /**
  * Default constrUCtor.
  */
  
  private HibernateSessionFactory() {
  }
  
  }
  在这个类中,用到了私有构造函数,如粗体部分.
  我的调用类:
  package zy.pro.td.plugin;
  
  /*
  * Created on Oct 4, 2004
  *
  * To change the template for this generated file go to
  * Window>Preferences>Java>Code Generation>Code and Comments
  */
  import javax.servlet.ServletException;
  
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.action.PlugIn;
  import org.apache.struts.config.ModuleConfig;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  
  import zy.pro.td.util.HibernateSessionFactory;
  
  /**
  * @author sunil
  *
  *  This class will initialize hibernate and bind SessionFactory in JNDI at the
  *  time of application and startup and unbind it from JNDI at the time of application
  * shutdown
  */
  public class HibernatePlugin
  implements PlugIn {
  
  private static final String jndi_hibernate = "jndi_hibernate_factory";
  private  HibernateSessionFactory hsf;
  private String name;
  
  public HibernatePlugin() {
  hsf=new HibernateSessionFactory();
  }
  
  // This method will be called at the time of application shutdown
  public void destroy() {
  System.out.println("Entering HibernatePlugIn.destroy()");
  //Put hibernate cleanup code here
  System.out.println("Exiting HibernatePlugIn.destroy()");
  }
  
  //This method will be called at the time of application startup
  public void init(ActionServlet actionServlet, ModuleConfig config) throws
  ServletException {
  System.out.println("Entering HibernatePlugIn.init()");
  System.out.println("Value of init parameter " + getName());
  //Uncomment next two lines if you want to throw UnavailableException from your servlet
  //    if(true)
  //      throw new ServletException("Error configuring HibernatePlugIn");
  System.out.println("Exiting HibernatePlugIn.init()");
  
  }
  
  private void bindFactoryToJNDI() {
  try {
  Context ctx = new InitialContext();
  
  }
  catch (Exception e) {
  e.printStackTrace();
  }
  }
  
  public String getName() {
  
  return name;
  }
  
  public void setName(String string) {
  name = string;
  }
  }
  在调用类中,我创建了一个HibernateSessionFactory的对象,但是在初始化时,却出了问题.总提示说:
  
  HibernateSessionFactory() has private access in zy.pro.td.util.HibernateSessionFactory at line 35(35:9)
  
  然后,我就将HibernateSessionFactory的构造函数由private改成了public,调试通过.
  
  构造函数为私有,就不能创建该类的对象.
上一篇:技术分析之——J2SE 5.0中的泛型 人气:750
下一篇:J2SE综合——对Final的一点认识 人气:503
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐