看下面的类:
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,调试通过.
构造函数为私有,就不能创建该类的对象.
闽公网安备 35060202000074号