如何使用XML配置Spring数据JPA

前端之家收集整理的这篇文章主要介绍了如何使用XML配置Spring数据JPA前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在读这本书“专业 Java for Web Applications – Nicholas S. Williams”
本书的示例对于Spring Data JPA具有此配置:
@Bean
public DataSource customerSupportDataSource()
{
    JndiDataSourceLookup lookup = new JndiDataSourceLookup();
    return lookup.getDataSource("jdbc/CustomerSupport");
}

@Bean
public LocalContainerEntityManagerfactorybean entityManagerfactorybean()
{
    Map<String,Object> properties = new Hashtable<>();
    properties.put("javax.persistence.schema-generation.database.action","none");
    properties.put("hibernate.ejb.use_class_enhancer","true");

    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setDatabasePlatform("org.hibernate.dialect.MysqL5InnoDBDialect");

    LocalContainerEntityManagerfactorybean factory =
            new LocalContainerEntityManagerfactorybean();
    factory.setJpaVendorAdapter(adapter);
    factory.setDataSource(this.customerSupportDataSource());
    factory.setPackagesToScan("com.wrox.site.entities","com.wrox.site.converters");
    factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
    factory.setValidationMode(ValidationMode.NONE);
    factory.setLoadTimeWeaver(this.loadTimeWeaver); // TODO: remove when SPR-10856 fixed
    factory.setJpaPropertyMap(properties);
    return factory;
}

@Bean
public PlatformTransactionManager jpaTransactionManager()
{
    return new JpaTransactionManager(
            this.entityManagerfactorybean().getObject()
    );
}

但是我一直在使用XML进行配置,我不知道如何将它翻译成只有XML,我到目前为止

<jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/test"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf" />
</bean>

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="com.test" />
</bean>

任何人可以指导我如何转换?

谢谢

如果要使用XML配置来配置Spring Data JPA(并使用本书中描述的配置),则必须按照以下步骤操作:

>配置数据源bean.
>配置实体管理器工厂bean.
>配置事务管理器bean.
启用注释驱动的事务管理.
>配置Spring Spring数据JPA.

应用程序上下文配置(applicationContext-persistence.xml)文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

  <!-- Configure the data source bean -->
  <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/CustomerSupport"/>

  <!-- Create default configuration for Hibernate -->
  <bean id="hibernateJpaVendorAdapter" 
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

  <!-- Configure the entity manager factory bean -->
  <bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <!-- Set JPA properties -->
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MysqL5InnoDBDialect</prop>
        <prop key="javax.persistence.schema-generation.database.action">none</prop>
        <prop key="hibernate.ejb.use_class_enhancer">true</prop>
      </props>
    </property>
    <!-- Set base package of your entities -->
    <property name="packagesToScan" value="foo.bar.model"/>
    <!-- Set share cache mode -->
    <property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
    <!-- Set validation mode -->
    <property name="validationMode" value="NONE"/>
  </bean>

  <!-- Configure the transaction manager bean -->
  <bean id="transactionManager" 
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>

  <!-- Enable annotation driven transaction management -->
  <tx:annotation-driven/>

  <!-- 
    Configure Spring Data JPA and set the base package of the 
    repository interfaces 
  -->
  <jpa:repositories base-package="foo.bar.repository"/>
</beans>

猜你在找的XML相关文章