2016-07-25 5 views
0

サービスクラスのautowired beanにNullPointerExceptionがあります。私がautowireしようとしているクラスは、カサンドラのリポジトリです。サービス中のNull Autowired Spring Bean(Cassandra Repository)

私の主なクラスApplication.java

@SpringBootApplication 
@EnableAutoConfiguration 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

マイCassandraの構成CassandraConfig.java

@Configuration 
@EnableCassandraRepositories(basePackages = "com.myretail") 
public class CassandraConfig extends AbstractCassandraConfiguration { 

    @Override 
    protected String getKeyspaceName() { 
     return "myretail"; 
    } 

    @Bean 
    public CassandraClusterFactoryBean cluster() { 
     CassandraClusterFactoryBean cluster = 
       new CassandraClusterFactoryBean(); 
     cluster.setContactPoints("127.0.0.1"); 
     cluster.setPort(9042); 
     return cluster; 
    } 

    @Bean 
    public CassandraMappingContext cassandraMapping() 
      throws ClassNotFoundException { 
     return new BasicCassandraMappingContext(); 
    } 

    @Bean 
    public ProductService productService() { 
     return new ProductService(); 
    } 
} 

マイ・リポジトリ(DAO)ProductPriceRepository.java

public interface ProductPriceRepository extends CassandraRepository<ProductPrice> { 

    @Query("select * from productprice where productId = ?0") 
    ProductPrice findByProductId(String productId); 
} 

マイサービスクラスProductService。 java

@Path("/product") 
@Component 
public class ProductService { 

    @Autowired 
    ProductPriceRepository productPriceRepository; 

    @GET 
    @Path("/{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Product getTargetProduct(@PathParam("id") String productId) { 
     String urlString = "https://api.vendor.com/products/v3/" + productId + "?fields=descriptions&id_type=TCIN&key=43cJWpLjH8Z8oR18KdrZDBKAgLLQKJjz"; 
     JSONObject json = null; 
     try { 
      json = new JSONObject(JsonReader.getExternalJsonResponse(urlString)); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     Product product = new Product(); 
     product.setId(productId); 
     try { 
      JSONObject productCompositeResponse = json.getJSONObject("product_composite_response"); 
      JSONArray items = productCompositeResponse.getJSONArray("items"); 
      JSONObject item = items.getJSONObject(0); 
      JSONObject onlineDescription = item.getJSONObject("online_description"); 
      product.setName(onlineDescription.getString("value")); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     ProductPrice productPrice = productPriceRepository.findByProductId(productId); 
     product.setProductPrice(productPrice); 

     return product; 
    } 
} 

私のpom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.myretail</groupId> 
    <artifactId>MyRetail</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>MyRetail</name> 

    <dependencies> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
     </dependency> 

     <dependency> 
      <groupId>com.datastax.cassandra</groupId> 
      <artifactId>cassandra-driver-core</artifactId> 
      <version>2.1.5</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-cassandra</artifactId> 
      <version>1.4.2.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.cassandraunit</groupId> 
      <artifactId>cassandra-unit-spring</artifactId> 
      <version>2.1.9.2</version> 
      <scope>test</scope> 
      <exclusions> 
       <exclusion> 
        <groupId>org.cassandraunit</groupId> 
        <artifactId>cassandra-unit</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

     <dependency> 
      <groupId>org.cassandraunit</groupId> 
      <artifactId>cassandra-unit-shaded</artifactId> 
      <version>2.1.9.2</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.hectorclient</groupId> 
      <artifactId>hector-core</artifactId> 
      <version>2.0-0</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-servlet</artifactId> 
      <version>1.18.3</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>1.18.3</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.3.1.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.3.1.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-autoconfigure</artifactId> 
      <version>1.3.6.RELEASE</version> 
     </dependency> 

    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.tomcat.maven</groupId> 
       <artifactId>tomcat7-maven-plugin</artifactId> 
       <version>2.2</version> 
       <configuration> 
        <url>http://localhost:8080/manager/text</url> 
        <server>my-tomcat</server> 
        <path>/myRetail</path> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

それは注釈がリポジトリをピックアップし、@EnableCassandraRepositories注釈のオフに基づいてBeanを作成する必要があることを私の理解です。この場合、のProductService.javaはいつもnullです。しかし、サービスコールに対してjunitテストを実行すると、Beanは正しく作成され、オブジェクトはnullではなく、テストは(@ContextConfigurationアノテーションを介して)渡されます。

私は助けてくれたと思われるいくつかのパターンを見てきましたが、どちらも効果がありませんでした。私はCassandraが内部的にそれを処理し、私はCassandraメソッドを実装することを余儀なくされているので、私のインターフェイスの実装を作成することはできません。

何かのアノテーションがちょっと残っているような気がします。何か案は?

+0

に依存関係や親ポンポンの下に含める必要がありますについては

pom.xmlあなたは注釈を付けているあなたにありますか? – Saravana

+0

はい、私はそれを取り上げましたが、私はそれを元に戻しました。以前に言及した依存関係の変更も行いました(その投稿は消えたように見えますか?)。 他の依存関係エラーの可能性があります。 –

+0

原因:org.springframework.beans.factory.BeanCreationException: 'productPriceRepository'という名前のBeanを作成中にエラーが発生しました:initメソッドの呼び出しに失敗しました。ネストされた例外はjava.lang.AbstractMethodErrorです:org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory $ CassandraQueryLookupStrategy.resolveQuery(Ljava/lang/reflect /メソッド; Lorg/springframework/data/repository/core/RepositoryMetadata; Lorg/springframework/data/repository/core/NamedQueries;)Lorg/springframework/data/repository/query/RepositoryQuery; –

答えて

0

問題は `@ Repository`アノテーションでProductPriceRepository`` spring-bootカサンドラアプリケーション、あなたがpom.xml

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.3.5.RELEASE</version> 
</parent> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-cassandra</artifactId> 
    </dependency> 
</dependencies> 
関連する問題