Spring Boot会话在存在2个或更多数据源时无法创建。

huangapple go评论162阅读模式
英文:

Springboot Session Cannot be created when 2 or more datasources exist

问题

OK - problem ==> 当使用Springboot Session时,2个或更多数据源会导致错误。

我有一个Springboot项目,为不同的目的使用不同的数据源。因此,在该项目中,我希望Spring使用Spring-Session模块的HttpSession替代HttpSession。

如果您遵循指南,会很容易创建会话表。
但是,指南和API文档说您只需要一个数据源。因此,由于我有2个或更多数据源,我会得到以下错误:

2020-07-21 21:38:59.134 ERROR 171262 --- [  restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter  : 
Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. 
Message: Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource 
[org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]: 
Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 'javax.sql.DataSource' available: 
expected single matching bean but found 2: authModelDs,secureSessionDS

因此,由于它无法决定使用哪个数据源,我必须手动配置它,但是整个过程会引导您到一个名为
org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration的类,根据需要设置自己的数据源,实际上是无法配置的。

我稍微查看了一下,发现一旦创建了Session Filter来替换数据源,就无法配置它。

因此,我很困惑:如何为配置的Spring-Session设置正确的数据源?

任何帮助都将不胜感激。

英文:

OK - problem ==> 2 Datasources (or more) will cause an error when using Springboot Session.

I have a Springboot project, with different data sources for different purposes. So within said project I would like to have Spring replace the HttpSession, with the Spring-Session module's HttpSession.

The creation of the session tables is simple if you follow the guides.
But the guides and API docs say you need JUST one data source. And so, since I have 2 or more I get the following error:

2020-07-21 21:38:59.134 ERROR 171262 --- [  restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter  : 
Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. 
Message: Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource 
[org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]: 
Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 'javax.sql.DataSource' available: 
expected single matching bean but found 2: authModelDs,secureSessionDS

So since it cannot decide which one to use, I have to manually configure it, except that the entire process leads you to a class called
org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration, which is quite literally non-configurable under the given need to set your own datasource.

I have looked a bit around, and there is no way to configure the Session Filter, once it is created to replace the datasource there too.

So I am boggled: how can I set the correct datasource for the configured Spring-Session?

Any help would be appreciated.

答案1

得分: 1

AHA!所以

有一个可以放在数据源Bean创建上的注解,用来指定这样的Bean是SpringBoot会话数据源。

该注解是 - @SpringSessionDataSource

在文档中并不是很直接的内容!

感谢以下的Stack Overflow回答:

https://stackoverflow.com/questions/49042664/spring-boot-and-spring-session-how-to-control-the-datasource#49052829

英文:

AHA! Soooooo

There is an annotation that you can place on the Bean creation of your datasource, to specify that such a bean is the SpringBoot session datasource.

The annotation is - @SpringSessionDataSource

It is not something that is really straight forward in the documentation at all!

Props to the following Stack Overflow answer:

https://stackoverflow.com/questions/49042664/spring-boot-and-spring-session-how-to-control-the-datasource#49052829

huangapple
  • 本文由 发表于 2020年7月22日 05:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63023229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定