Hibernate 通过jar文件加载映射文件

配置文件这样写:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" depends-on="scstDatasource">
		<!-- <property name="dataSource" ref="dataSource" /> -->
		<property name="dataSource" ref="scstDatasource" />
        <property name="mappingJarLocations">
            <list>
                <!-- Also change the mail sender mode -->
                <!-- 以下配置用于测试环境 
                <value>file:WebContent/WEB-INF/lib/jbpm-3.1.4.jar</value>
                <value>file:WebContent/WEB-INF/lib/jbpm-identity-3.1.4.jar</value>
                <value>file:WebContent/WEB-INF/lib/cdl-scst-hbm.jar</value>
				-->
				
                <!-- 以下配置用于生产环境 -->
                <value>WEB-INF/lib/jbpm-3.1.4.jar</value>
                <value>WEB-INF/lib/jbpm-identity-3.1.4.jar</value>
                <value>WEB-INF/lib/cdl-scst-hbm.jar</value>

            </list>
        </property>
.......剩余部分省略


Hibernate 是通过以下源码来加载jar文件,完成上面的配置:
public Configuration addJar(File jar) throws MappingException {   
        log.info( "Searching for mapping documents in jar: " + jar.getName() );   
        JarFile jarFile = null;   
        try {   
            try {   
                jarFile = new JarFile( jar );   
            }   
            catch (IOException ioe) {   
                throw new InvalidMappingException(   
                        "Could not read mapping documents from jar: " + jar.getName(), "jar", jar.getName(),   
                        ioe   
                );   
            }   
            Enumeration jarEntries = jarFile.entries();   
            while ( jarEntries.hasMoreElements() ) {   
                ZipEntry ze = (ZipEntry) jarEntries.nextElement();   
                if ( ze.getName().endsWith( ".hbm.xml" ) ) {   
                    log.info( "Found mapping document in jar: " + ze.getName() );   
                    try {   
                        addInputStream( jarFile.getInputStream( ze ) );   
                    }   
                    catch (Exception e) {   
                        throw new InvalidMappingException(   
                                "Could not read mapping documents from jar: " + jar.getName(),   
                                "jar",   
                                jar.getName(),   
                                e   
                        );   
                    }   
                }   
            }   
        }   
        finally {   
            try {   
                if ( jarFile != null ) {   
                    jarFile.close();   
                }   
            }   
            catch (IOException ioe) {   
                log.error("could not close jar", ioe);   
            }   
        }   
  
        return this;   
    }  

猜你喜欢

转载自j2ee-zhongqi.iteye.com/blog/1111305
今日推荐