我有一个Spring Boot 1.5应用程序,我在pom.xml中添加了Spring-Session。我正在实例化CookieSerializer
类的2个实例,以满足我在配置类中的需要。
但是当应用程序启动时,我收到以下错误消息:
Parameter 0 of method setCookieSerializer in org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration required a single bean, but 2 were found:
- authCookieSerializer: defined by method 'authCookieSerializer' in class path resource [nextgen/config/SpringHttpSessionConfig.class]
- sessionCookieSerializer: defined in BeanDefinition defined in class path resource [nextgen/config/SpringHttpSessionConfig.class]
如果我想将CookieSerializer
bean用于其他事情,我如何使用SpringSession?我是否被迫将CookieSerializer
定义为@Primary
bean才能自动连接到SpringSession配置类中?
我的配置类:
@Configuration
public class SpringHttpSessionConfig {
private Logger logger = LoggerFactory.getLogger(SpringHttpSessionConfig.class);
@Bean(name="authCookieSerializer")
public CookieSerializer authCookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("auth");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
serializer.setCookiePath("/");
return serializer;
}
@Bean(name="sessionClassicCookieSerializer")
public CookieSerializer sessionCookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("JSESSIONID");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
serializer.setCookiePath("/classic");
return serializer;
}
application.properties:
spring.session.store-type=hash-map
我错过了一些明显的东西吗?或者更确切地说,我对SpringSession配置需要什么定义CookieSerializer,以及如何为Spring Session配置bean指定它而不是" primary"豆?或者我别无选择,只能定义" primary" CookieSerializer是我想要注入SpringHttpSessionConfiguration
类的那个?