我在服务类中的自动装配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 ProductPriceRepository
在ProductService.java
总是null
虽然当我在tomcat上运行这个。但是,如果我针对服务调用运行junit测试,则该bean已正确创建,该对象不为空,并且测试通过(通过@ContextConfiguration
注释)。
我已经看过几种不同的模式,我认为可能会有帮助,但他们都没有工作。我无法创建我的界面实现,因为Cassandra在内部处理这个界面,我不得不实施Cassandra方法。
我觉得某些地方的注释稍微有些偏离。有任何想法吗?
你注释'ProductPriceRepository'与'@ Repository'注解? – Saravana
是的,我已经把它拿出来了,但是我又加入了它。我还做了你之前提到的依赖关系更改(该帖子看起来像消失了??),现在我得到下面的错误。 看起来像另一个依赖错误也许... –
引起: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; –