在将项目 spring boot 从 2.0.5.RELEASE 升级到 2.1.18.RELEASE 并将 spring 框架从 5.0.9.RELEASE 升级到 5.1.20.RELEASE 之后,应用程序在启动运行时得到了UnsatisfiedDependencyException :
“调用 init 方法失败;嵌套异常是 java.lang.IllegalStateException:您在存储库中定义了查询方法,但没有定义任何查询查找策略。基础设施显然不支持查询方法!”
这个项目使用 redis 作为持久性与 spring-data-redis 和 spring-data-jpa 与 SQL Server。Redis仓库实现了查询方法:
@EnableAspectJAutoProxy
@PropertySource("classpath:/application.properties")
@PropertySource(value = "classpath:application-${project_env:default}.properties", ignoreResourceNotFound = true)
@PropertySource(value = "file:/project/application.properties", ignoreResourceNotFound = true)
@EnableRedisRepositories("project.repository.redis")
@Configuration
public class MainConfiguration { }
package project.repository.redis;
import project.domain.redis.hash.SessionRegistry;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
public interface ProjectRedisRepository extends CrudRepository<SessionRegistry, String> {
SessionRegistry findBySessionKey(String sessionKey);
}
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
import project.domain.enums.SessionStatus;
@RedisHash(timeToLive = 120)
public class SessionRegistry {
@Id
private String id;
@Indexed
private String sessionKey;
private SessionStatus status;
private String token;
public SessionRegistry() {
}
...getters and setters...
构建.gradle
dependencies {
...
// In case of Database use
compile "org.springframework.boot:spring-boot-starter-data-jpa:2.1.18.RELEASE"
compile "org.springframework:spring-jdbc:5.1.20.RELEASE"
runtime "com.microsoft.sqlserver:mssql-jdbc:9.1.0.jre11-preview"
compile "com.zaxxer:HikariCP:3.1.0" // connection pool
...
仅当存储库具有查询方法时才会发生此错误。如果我们使用默认的 CrudRepository 方法,就没有问题。
依赖项:
| +--- org.springframework.boot:spring-boot:2.0.6.RELEASE -> 2.1.18.RELEASE (*)
| +--- org.springframework.boot:spring-boot-starter-data-redis:2.0.4.RELEASE -> 2.1.18.RELEASE
| | +--- org.springframework.boot:spring-boot-starter:2.1.18.RELEASE (*)
| | +--- org.springframework.data:spring-data-redis:2.1.21.RELEASE
| | | +--- org.springframework.data:spring-data-keyvalue:2.1.21.RELEASE
| | | | +--- org.springframework.data:spring-data-commons:2.1.21.RELEASE (*)
| +--- org.springframework.data:spring-data-jpa:2.1.21.RELEASE
| | +--- org.springframework.data:spring-data-commons:2.1.21.RELEASE
| | | +--- org.springframework:spring-core:5.1.19.RELEASE (*)
| | | +--- org.springframework:spring-beans:5.1.19.RELEASE (*)
由于 Cloud Open Feing 依赖升级和兼容性,降级Spring Boot 和 Spring Framework 不是该项目的选项。