当前位置:首页 > 开发 > 编程语言 > Hibernate > 正文

HIbernate的单例例子

发表于: 2013-11-09   作者:alleni123   来源:转载   浏览次数:
摘要: 例子一: public final class XMLHelper { public static final EntityResolver DEFAULT_DTD_RESOLVER = new DTDEntityResolver(); private DOMReader domReader; private SAXReader saxReader;
例子一:

 

public final class XMLHelper {

	public static final EntityResolver DEFAULT_DTD_RESOLVER = new DTDEntityResolver();

	private DOMReader domReader;
	private SAXReader saxReader;

	/**
	 * @param errorHandler the sax error handler
	 * @param entityResolver an xml entity resolver
	 *
	 * @return Create and return a dom4j {@code SAXReader} which will append all validation errors
	 *         to the passed error list
	 */
	public SAXReader createSAXReader(ErrorHandler errorHandler, EntityResolver entityResolver) {
		SAXReader saxReader = resolveSAXReader();
		saxReader.setEntityResolver( entityResolver );
		saxReader.setErrorHandler( errorHandler );
		return saxReader;
	}

	private SAXReader resolveSAXReader() {
		if ( saxReader == null ) {
			saxReader = new SAXReader();
			saxReader.setMergeAdjacentText( true );
			saxReader.setValidation( true );
		}
		return saxReader;
	}

	
	
	
}



这里在createSAXReader方法的第一行就调用了resolveSAXReader(),
后者检查了saxReader是否为空, 如果为空, 则创建新的。
不为空则返回类的内部实例saxReader.



========================================================================

例子二:

package org.hibernate.internal.util;

/**
 * This exists purely to allow custom ClassLoaders to be injected and used
 * prior to ServiceRegistry and ClassLoadingService existence.  This should be
 * replaced in Hibernate 5.
 * 
 * @author Brett Meyer
 */
public class ClassLoaderHelper {
	
	public static ClassLoader overridenClassLoader = null;
	
	public static ClassLoader getContextClassLoader() {
		return overridenClassLoader != null ?
			overridenClassLoader : Thread.currentThread().getContextClassLoader();
	}
}


这里通过thread获取了Context classloader
官方api如下:

Returns the context ClassLoader for this Thread. The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not set, the default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application.

If a security manager is present, and the invoker's class loader is not null and is not the same as or an ancestor of the context class loader, then this method invokes the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to verify that retrieval of the context class loader is permitted.

Returns:
the context ClassLoader for this Thread, or null indicating the system class loader (or, failing that, the bootstrap class loader)
Throws:
SecurityException - if the current thread cannot get the context ClassLoader
Since:
1.2

HIbernate的单例例子

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
摘要:本文深入浅出的讲述了设计模式中的 单例 模式 , 并给出了简单的示例 , 例子浅显易懂 , 并附带
大家都知道的一件事情,Hibernate启动时需要加载配置文件,连接数据库并映射对象,这一过程需要大量
大家都知道的一件事情,Hibernate启动时需要加载配置文件,连接数据库并映射对象,这一过程需要大量
在使用Spring3对控制器Controller进行bean管理时,如果要对控制器是否单例进行管理。 有两种方式配
保证一个类只有一个实例,并提供一个访问它的全局访问点。 类图: 代码: template<typename T&g
java设计模式中有一个模式叫单例模式,单例的意思就是该类只能产生一个实例,平时我们定义类可以随
原文地址:http://www.cnblogs.com/Ray-xujianguo/p/3396884.html 在讲单例模式之前,我想先试讲一
在讲单例模式之前,我想先试讲一个例子,在什么程序中,我们都需要读取property配置文件,通过Java
单例模式 Singletonn Pattern Ensure a class has only one instance, and provide a global point
一. 单例模式 整理自 《java与模式》阎宏编著 1.意图: 确保某个类只有一个实例,并且为该类提供一
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号