[英]Spring Data Redis NoSuchBeanDefinitionException: No qualifying bean of type
[英]Error in Spring boot Session with Redis - No qualifying bean of type […SessionRepository]
我正在遵循官方的Spring教程,将Redis会话支持添加到Spring Boot。
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html
的pom.xml
...
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
...
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
...
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
...
从1.3.0.RELEASE
,我没有在Spring会话中添加该版本1.3.0.RELEASE
Boot的发布已经包含了jar。 即使按照教程添加版本1.0.2.RELEASE
也无法解决我的问题
请注意,使用spring-boot 1.3.1.RELEASE
,使用的春季版本是4.2.4.RELEASE
组态
@EnableRedisHttpSession
public class HttpSessionConfig {
}
属性文件
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
我没有添加密码,因为我的Redis服务器密码为空。 甚至添加密码也无法解决我的问题。
当我运行应用程序时,出现以下错误
原因:..NoSuchBeanDefinitionException:未找到依赖项为[... SessionRepository]类型的合格Bean:Expect ... endency。 依赖注释:{}
我还为下面的参考添加了完整的错误堆栈
016-01-05 01:49:50.775 ERROR 7616 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
...
at com.enbiso.proj.estudo.Application.main(Application.java:25) [classes/:na]
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:99) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
...
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
... 8 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionRepositoryFilter' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.session.SessionRepository]: : No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
...
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_20]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_20]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
...
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 27 common frames omitted
您缺少创建RedisConnectionFactory步骤。
尝试这个 :
@EnableRedisHttpSession
public class HttpSessionConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
}
这些是我要解决此问题的步骤:
1-application.properties:
spring.session.store-type=redis
spring.redis.host=localhost
spring.redis.password=Password123OrSomething
spring.redis.port=1234
2-pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3-Redis配置类(Spring Boot):
@ConditionalOnProperty(name = "spring.session.store-type", havingValue = "redis")
@EnableRedisHttpSession
public class RedisSessionConfig {
@Value("${spring.session.store-type}")
private String sessionStoreType;
private static final Logger LOGGER = LoggerFactory.getLogger(RedisSessionConfig.class);
@PostConstruct
public void init() {
LOGGER.info("spring.session.store-type=none turns spring session off.");
LOGGER.info("Redis Session Replication is turned {}.", sessionStoreType.equals("redis") ? "ON"
: "OFF");
}
@Bean
public ConfigureRedisAction configureRedisAction() {
LOGGER.info("Preventing auto-configuration in secured environments.");
return ConfigureRedisAction.NO_OP;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.