在我的项目中,我通过实现CrudRepository创建了一个存储库接口 . 我的底层数据库是Cassandra . 我间歇性地得到以下错误,我的应用程序无法启动 -
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userHelper': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.org.retail.userops.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
还有其他存储库接口正常工作 .
我的 Spring 季启动应用 -
@SpringBootApplication
@Configuration
@ComponentScan( basePackages = "com.org.retail")
@EnableCassandraRepositories( basePackages = { "com.org.retail.userops.repository" })
public class UserApplication {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main( String[] args) {
SpringApplication.run(UserApplication.class, args);
}
/**
* Rest template.
*
* @param builder
* the builder
* @return the rest template
*/
@Bean
public RestTemplate restTemplate( RestTemplateBuilder builder) {
return builder.build();
}
}
这是UserRepository接口 -
package com.org.retail.userops.repository;
import java.util.List;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.org.retail.userops.domain.User;
public interface UserRepository extends CrudRepository<User, String> {
@Query( "select * from user where userid=?0 and userstatus IN (?1)")
List<User> findByUserIdAndUserStatus( String userId, List<String> statusList);
}
有时候应用程序会起来,有时候不会,所以很奇怪 . 我怀疑在加载存储库时是否存在任何Cassandra错误 . 有没有办法在Spring启动时看到sql错误?
1 回答
在UserRepository类之上添加
@Repository
(或@Service
或@Component
),它可能会解决问题 .但如果问题只是间歇性的,那么本地机器上的cassandra DB可能存在一些连接问题 .
要查看要查询的内容,请在
application.properties
文件中添加这些查询要查看 Spring 天在幕后或启动时正在做什么,请添加以下内容: