java - Spring - 在不使用 Autowiring 的情况下在 xml-config 中注入(inject) JpaRepository

标签 java spring hibernate jpa

我目前正在着手一个结合使用 spring-data 和 JPA/Hibernate 的项目。 现在,我在相关属性上使用 @Autowired 注释注入(inject) JpaRepositories,例如:

@Component
public class EmployeeGenerator implements IDataGenerator {
...
    @Autowired
    private IEmployeeDao        dao;
...
}

.. 其中 IEmployeeDao 是一个扩展 JpaRepository 的接口(interface),被注释为 @Repository:

@Repository
public interface IEmployeeDao extends JpaRepository<Employee, Integer> {

     /**
     * Finds employees by username.
     *
     * @param username the username
     * @return the list of employees
     */
    List<Employee> findByUsername(String username);

使用这种方法一切正常 - 然而,我更习惯于使用 XML 完成我的大部分 spring 配置工作,因为我个人喜欢所有相关配置都在同一个地方并且第一眼可见的想法。

现在,据我了解 JPA 和 spring-data,存储库实例是由 JPA 实体管理器以某种方式创建的,因此我应该能够使用某种工厂将它们指定为 spring config xml 中的 beans方法? 我想我正在寻找类似以下内容的东西:

<import resource="classpath:spring/db-context.xml"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="..."/>
    <property name="dataSource" ref="..."/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
        </props>
    </property>
</bean>
...
<bean id="employeeDaoImpl" class="IEmployeeDao">
     <factory-method="?????"> <!-- Is something like this possible??? -->
</bean>

经过一些阅读,我猜想 Autowiring 存储库是“推荐”的方法,而且我确实看到这样做有一些好处, 但仍然出于兴趣,我想让它使用纯 xml 配置(或者至少没有 @Autowired,也就是说)

最佳答案

您可以使用 <jpa:repositories /> 声明存储库.然后您可以在 XML 配置中使用存储库引用。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/data/jpa"
  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.xsd">

  <repositories base-package="com.acme.repositories" />

</beans:beans>

In this case we instruct Spring to scan com.acme.repositories and all its sub packages for interfaces extending Repository or one of its sub-interfaces. For each interface found it will register the persistence technology specific FactoryBean to create the according proxies that handle invocations of the query methods. Each of these beans will be registered under a bean name that is derived from the interface name, so an interface of UserRepository would be registered under userRepository. The base-package attribute allows the use of wildcards, so that you can have a pattern of scanned packages.

您可以在文档中阅读更多相关信息:http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/repositories.html#repositories.create-instances

关于java - Spring - 在不使用 Autowiring 的情况下在 xml-config 中注入(inject) JpaRepository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995450/

相关文章:

java - PBEKeySpec iterationCount 和 keyLength 参数有什么影响?

java - JPQL——空值到 boolean 值?

hibernate 映射异常 : Repeated column in mapping for entity

java - 更新 hibernate 中的查询

java - Spring Tx 错误

javax.validation.ConstraintDefinitionException : HV000074

java - 如何在tomcat中配置jdbc连接池并在jersey中使用

java - 如何更改 Gradle IntelliJ 项目中的依赖关系?

java - 组合和聚合关系 UML 存在问题

java - 调用 @PostPersist 时数据未保存在数据库中