Spring数据jpa - 存储库的自动装配不起作用

时间:2013-07-09 07:17:20

标签: spring spring-data spring-data-jpa

我在JPA中使用spring数据。我用于XML文件:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
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/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:component-scan base-package="com.aa.bb" />
<bean id="exceptionPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<context:property-placeholder location="classpath:bb.properties"/>

<!-- DATA BASE -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="spiedDataSource" />
    <property name="packagesToScan" value="com.aa.bb.cc.domain" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="database" value="SQL_SERVER" />
        </bean>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClass}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.user}" />
    <property name="password" value="${jdbc.pwd}" />
</bean>

<bean id="spiedDataSource" class="com.p6spy.engine.spy.P6DataSource">
    <constructor-arg>
        <ref local="dataSource"/>
    </constructor-arg>
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager" />

<jpa:repositories base-package="com.aa.bb.cc.repository" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
</beans>

另一个用于石英计时的bean xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
xsi:schemaLocation="
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="dailyMailJob" class="com.aa.bb.cc.timer.DailyMailJob">
    <property name="mailSender" ref="mailSender" />
</bean>
<bean id="mailSender" class="com.aa.bb.cc.util.MailSender">
    <!-- <property name="mailsList" value="${bb.properties.mail.list}"/> -->
</bean>

<bean id="runMeJob"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="dailyMailJob" />
    <property name="targetMethod" value="sendDailyMail" />
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="runMeJob" />
    <property name="cronExpression" value="5 * * * * ?" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="runMeJob" />
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>
</bean>

现在,我尝试将Repository接口自动装配到第二个XML文件上声明的类:

public class DailyMailJob {

@Autowired
private MessageNotifyRepository messageNotifyRepository;

private MailSender mailSender;
...

我在messageNotifyRepository字段上获取NULL。

我该如何解决这个问题?这似乎是错误的配置。

2 个答案:

答案 0 :(得分:1)

好吧,我找到了解决方案。

这是一个非常简单和基本的春季问题,我可能错过了它,因为我搞砸了存储库。

我所要做的就是在第一个XML文件上添加<import resource="XML2" />

非常感谢...

答案 1 :(得分:0)

我相信你错过了一段配置:

<?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.aa.bb.???" />

</beans:beans>

其中???应该是查找Data JPA存储库的包。有关详细信息,请参阅the reference