【问题标题】:Spring CrudRepository is null for @Autowired member variable@Autowired 成员变量的 Spring CrudRepository 为 null
【发布时间】:2012-12-25 18:30:05
【问题描述】:

我是 Spring JPA 的新手。我正在尝试使用休眠+ Spring JPA。为此,我在存储库下面创建了

package com.raptorservice.da.dal;

import com.raptorservice.da.objects.ClientDO;
import org.springframework.data.repository.CrudRepository;

public interface ClientRepository extends CrudRepository<ClientDO, String> {    
}

下面是我的 spring-ws-servlet.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:sws="http://www.springframework.org/schema/web-services"
    xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="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/web-services 
                        http://www.springframework.org/schema/web-services/web-services.xsd 
                        http://www.springframework.org/schema/oxm 
                        http://www.springframework.org/schema/oxm/spring-oxm.xsd
                        http://www.springframework.org/schema/jdbc
                        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                        http://www.springframework.org/schema/data/jpa
                        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                        ">

    <!--  ****************************** BASIC CONFIGURATION ******************************************** -->
    <!-- Enable annotations for end points -->
    <sws:annotation-driven />


    <bean id="getpointsbean" class="com.raptorservice.endpoints.GetPointsEndpoint">
    </bean>

    <sws:dynamic-wsdl id="points" portTypeName="Points" locationUri="/points/" targetNamespace="http://sailin.com/schemas">
        <sws:xsd location="/WEB-INF/xsd/points.xsd" />
    </sws:dynamic-wsdl>

    <!--  Configure JAXB to marshal and un-marshal requests -->
    <oxm:jaxb2-marshaller id="jaxbMarshaller"  contextPath="com.raptorservice.generated" />



    <!-- ******************************* PERSISTANCE CONFIGURATION ****************************************** -->
    <!--  JPA Repositories -->
<jpa:repositories  base-package="com.raptorservice.da.dal" /> 



    <!--  Hibernate Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/testservice" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceXmlLocation" value="./WEB-INF/persistence.xml"></property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
            </bean>
        </property>
     </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
<!--        <property name="configLocation"> -->
<!--            <value>classpath:hibernate.cfg.xml</value> -->
<!--        </property> -->
<!--        <property name="configurationClass"> -->
<!--            <value>org.hibernate.cfg.AnnotationConfiguration</value> -->
<!--        </property> -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>



 </beans>

我在 WEB-INF 下也有一个有效的 persistence.xml 文件。

我正在尝试在我的网络服务 (@Endpoint) 中使用 ClientRepository,如下所示

package com.raptorservice.endpoints;


@Endpoint
public class GetPointsEndpoint {    

    @Autowired
    private ClientRepository clientRepository;

@PayloadRoot(localPart="getPointsForClientAndCustomerRequest", namespace="http://sailin.com/schemas")
    @ResponsePayload()
    public GetPointsForClientAndCustomerResponse getPointsForClientAndCustomerRequest(@RequestPayload GetPointsForClientAndCustomerRequest getpointsRequest) throws Exception
    {
        GetPointsForClientAndCustomerResponse resp = new GetPointsForClientAndCustomerResponse();
        ResultCode resCode = new ResultCode();

        try
        {
        ClientDO client = new ClientDO();
        client.setName("Test1");
        client.setClientId("test111");
        clientRepository.save(client);
        }
        catch(Exception ex)
        {
          System.out.println(ex);
        }


        resCode.setDescription("pass");
        resCode.setCode(BigInteger.valueOf(5));
        resp.setResultCode(resCode);
        return resp;
    }

但 clientRepository 始终为空。我错过了什么?为什么 clientRepository 没有自动连接?

我检查了日志,没有什么明显的。请帮忙。

提前致谢。

【问题讨论】:

  • 您是如何解决问题的?

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


【解决方案1】:

我认为缺少&lt;context:annotation-config/&gt;,需要这个注解在应用上下文中注册一个AutowiredAnnotationBeanPostProcessor实例。

AutowiredAnnotationBeanPostProcessor 如果您使用过,也会隐式注册在应用上下文中 &lt;context:component-scan ...&gt; xml 配置中的注解。

Difference between <context:annotation-config> vs <context:component-scan>

通常,如果字段值为 null 并且没有发生异常,则意味着该对象不是 Spring bean(在其他地方实例化),或者它没有由 IoC 容器进行后处理(可能后处理器注册在 @ 987654324@)。

由于 bean 被急切地初始化为单例,并且 @Autowired 注释的 required 属性默认设置为 true,如果 bean 被处理,Spring 很可能在容器初始化期间抛出异常。

【讨论】:

  • 您好鲍里斯,感谢您的回复。添加 后,我看到它至少尝试注入我的成员变量。但是,它未能抱怨其中一个 bean 未注册。我想我必须为此研究组件扫描。但是感谢您的提示和链接。
猜你喜欢
  • 1970-01-01
  • 2020-10-11
  • 2014-12-15
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多