2016-07-25 96 views
0

我在服務類中的自動裝配bean上獲得NullPointerException。我嘗試自動裝配的類是Cassandra存儲庫。Null Autowired Spring Bean(Cassandra存儲庫)在服務

我的主類Application.java

@SpringBootApplication 
@EnableAutoConfiguration 
public class Application { 

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

我卡桑德拉配置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註釋的豆。 @Autowired ProductPriceRepositoryProductService.java總是null雖然當我在tomcat上運行這個。但是,如果我針對服務調用運行junit測試,則該bean已正確創建,該對象不爲空,並且測試通過(通過@ContextConfiguration註釋)。

我已經看過幾種不同的模式,我認爲可能會有幫助,但他們都沒有工作。我無法創建我的界面實現,因爲Cassandra在內部處理這個界面,我不得不實施Cassandra方法。

我覺得某些地方的註釋稍微有些偏離。有任何想法嗎?

+0

你註釋'ProductPriceRepository'與'@ Repository'註解? – 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/Method; Lorg/springframework/data/repository/core/RepositoryMetadata; Lorg/springframework /數據/庫/型芯/ NamedQueries)Lorg/springframework的/數據/庫/查詢/ RepositoryQuery; –

回答

0

問題是與你的pom.xml

對於spring-boot卡桑德拉的應用程序,你必須包括以下依賴和父POM在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>