繁体   English   中英

空的自动接线Spring Bean(Cassandra存储库)在使用中

[英]Null Autowired Spring Bean (Cassandra Repository) in Service

我在服务类中的自动装配的bean上收到NullPointerException 我尝试自动连线的类是Cassandra存储库。

我的主类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。 尽管在Tomcat上运行时, ProductService.java@Autowired ProductPriceRepository始终为null 但是,如果我对服务调用运行junit测试,则正确创建了bean,该对象不为null,并且测试通过了(通过@ContextConfiguration批注)。

我看过几种我认为可能会有所帮助的不同模式,但是它们都不起作用。 我无法创建接口的实现,因为Cassandra在内部进行了处理,因此我不得不实现Cassandra方法。

我觉得某些地方的注释稍微有些偏离。 有任何想法吗?

问题出在你的pom.xml

对于spring-boot Cassandra应用程序,您必须在pom.xml包括以下依赖项和父pom

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM