1

我在 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。

我怎样才能解决这个问题?这似乎是错误的配置。

4

2 回答 2

1

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

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

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

不管怎么说,还是要谢谢你...

我愿意

于 2013-07-09T09:02:28.643 回答
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 存储库的包。有关详细信息,请参阅参考资料

于 2013-07-09T07:50:47.763 回答