2

私は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 ファイルがあります。

以下のように、Web サービス (@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 は常に null です。私は何が欠けていますか?clientRepository が自動配線されないのはなぜですか?

ログを確認しましたが、明らかなことは何もありません。助けてください。

前もって感謝します。

4

1 に答える 1

2

それが欠けていると思います<context:annotation-config/>。このアノテーションは、アプリケーションコンテキストにAutowiredAnnotationBeanPostProcessorインスタンスを登録するために必要です。

AutowiredAnnotationBeanPostProcessor<context:component-scan ...>も、xml 構成でアノテーションを使用した場合、アプリ コンテキストに暗黙的に登録され ます。

<context:annotation-config> と <context:component-scan> の違いを参照してください

通常、フィールド値が null で例外が発生しない場合は、オブジェクトが Spring Bean ではない (別の場所でインスタンス化されている) か、IoC コンテナーによって後処理されていない (ポスト プロセッサが別のコンテキストに登録されている可能性があります) ことを意味します。

Bean は熱心に初期化されたシングルトンであり、@Autowiredアノテーションのrequired属性はデフォルトで true に設定されているため、Bean がまったく処理されていない場合、Spring はコンテナーの初期化中に例外をスローする可能性が高くなります。

于 2012-12-25T20:26:42.450 に答える