---
title: "Java在各种框架下编译Scala 【三】"
author: "keriny"
date: "2011-02-15T10:09:46Z"
tags: []
categories: ["编程语言"]
slug: "java-scala"
draft: false
lastmod: "2011-02-15T10:09:46Z"
id: 906451
url: "https://keriny.iteye.com/blog/906451"
---

**设置Spring bean访问和调用的数据库**

要定义这些Spring bean，创建main/resources的beans.xml以及一下的内容：

```

<?xml version="1.0" encoding="UTF-8" ?>         
<beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"          
          
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
       xmlns:context="http://www.springframework.org/schema/context"          
          
        xmlns:tx="http://www.springframework.org/schema/tx"         
       xsi:schemaLocation="http://www.springframework.org/schema/beans          
          
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          
         http://www.springframework.org/schema/context         
          
        http://www.springframework.org/schema/context/spring-context-3.0.xsd          
         http://www.springframework.org/schema/tx         
          
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">          
     <context:component-scan base-package="com.foo.myapp"/>         
          
    <bean id="emf"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">          
        <property name="persistenceXmlLocation"value="classpath:/META-INF/my-persistence.xml"/>          
     </bean>         
    <bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">          
          
        <property name="entityManagerFactory" ref="emf"/>          
     </bean>         
          
    <tx:annotation-driven />          
</beans> 
```

读取数据库配置中persistence.xml文件。在这里，您将获得一个主目录中名为MyApp的H2数据库。因此，创建main/resources/META-INF文件的内容如下。

```

<persistencexmlnspersistencexmlns="http://java.sun.com/xml/ns/persistence"          
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
          
            xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"           
             version="2.0">          
          
   <persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">           
        <provider>org.hibernate.ejb.HibernatePersistence</provider>          
          
        <properties>          
           <propertynamepropertyname="hibernate.connection.driver_class"value="org.h2.Driver"/>           
          
           <property name="hibernate.connection.url"value="jdbc:h2:tcp://localhost/~/myapp"/>           
            <propertynamepropertyname="hibernate.connection.username" value="sa"/>          
          
            <propertynamepropertyname="hibernate.connection.password" value=""/>          
           <property name="hibernate.dialect"value="org.hibernate.dialect.H2Dialect"/>           
          
           <property name="hibernate.hbm2ddl.auto"value="create"/>           
        </properties>          
          
   </persistence-unit>           
/persistence>       
```