2016-07-25 2 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); 
    } 
} 

내 카산드라 구성 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

012 3,516,
@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 주석의 기반으로 빈을 만드는 것이 나의 이해이다. 의 ProductService.java은 내가 바람둥이에서 실행할 때 항상 null입니다. 그러나 서비스 호출에 대해 junit 테스트를 실행하면 bean이 제대로 생성되고 객체가 null이 아니며 테스트가 (@ContextConfiguration annotation을 통해) 통과합니다.

나는 도움이 될 것으로 생각되는 몇 가지 패턴을 보았지만 그 중 누구도 일하지 못했습니다. Cassandra가 내부적으로 처리하고 Cassandra 메소드를 구현해야하기 때문에 인터페이스 구현을 만들 수 없습니다.

어딘가에 어노테이션이 약간 붙어있는 것처럼 느낍니다. 어떤 아이디어?

+0

에 의존하고 부모 치어 아래에 포함 할 필요가 들어

pom.xml 당신이 주석 한 당신과 함께 무엇입니까? – Saravana

+0

그래, 나는 그것을 밖으로 가지고 있지만 그것을 다시 추가했습니다. 나는 또한 당신이 이전에 언급 한 의존성 변화를 만들었습니다. (그 게시물은 사라진 것처럼 보입니다.) 그리고 이제 아래에 오류가 나타납니다. 다른 종속성 오류가 발생할 수 있습니다 ... –

+0

발생 원인 : org.springframework.beans.factory.BeanCreationException : 'productPriceRepository'라는 이름의 빈을 만드는 동안 오류가 발생했습니다 : 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)입니다./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> 
관련 문제