JdbcOperationsSessionRepository.jdbcSession 不可见
JdbcOperationsSessionRepository.jdbcSession is not visible
我的程序正在使用 Spring Session xml based configuration and I using JdbcOperationsSessionRepository to implement my session. The JdbcOperationsSessionRepository 库正在使用 JdbcOperationsSessionRepository.JdbcSession 我如何设置会话属性?
package sessioncontrol.page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.Session;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession;
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Controller
@EnableJdbcHttpSession
public class SessionControl {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
PlatformTransactionManager transactionManager;
private JdbcOperationsSessionRepository repository;
@RequestMapping(value="flpage", method=RequestMethod.GET)
public String showPage(Model model) {
repository = new JdbcOperationsSessionRepository(jdbcTemplate, transactionManager);
repository.setTableName("test.spring_session");
repository.setDefaultMaxInactiveInterval(120);
JdbcSession session = repository.createSession();
session.setAttribute("ATTR_USER", "rwinch");
repository.save(session);
return "flpage";
}
}
它告诉我导入 org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession;
不可见 那么我怎样才能正确使用内部 class 设置属性方法?我真的卡在这里了。
提前致谢,如有任何意见,我们将不胜感激。
首先,您应该使用接口(SessionRepository
或 FindByIndexNameSessionRepository
)与您的存储库交互并注入由 Spring 会话配置创建并注册为应用程序上下文中的一个 bean,而不是自己实例化 JdbcOperationsSessionRepository
。
基本上有两种方法可以注入 FindByIndexNameSessionRepository
实例 - 注入并使用原始类型,或参数化(尊重 FindByIndexNameSessionRepository<S extends Session>
的原始契约)。
原始类型方法:
class RawConsumer {
@Autowired
private FindByIndexNameSessionRepository sessionRepository;
void consume() {
Session session = (Session) this.sessionRepository.createSession();
session.setAttribute("test", UUID.randomUUID().toString());
this.sessionRepository.save(session);
}
}
参数化类型方法:
class ParameterizedConsumer<S extends Session> {
@Autowired
private FindByIndexNameSessionRepository<S> sessionRepository;
void consume() {
S session = this.sessionRepository.createSession();
session.setAttribute("test", UUID.randomUUID().toString());
this.sessionRepository.save(session);
}
}
我的程序正在使用 Spring Session xml based configuration and I using JdbcOperationsSessionRepository to implement my session. The JdbcOperationsSessionRepository 库正在使用 JdbcOperationsSessionRepository.JdbcSession 我如何设置会话属性?
package sessioncontrol.page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.Session;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession;
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Controller
@EnableJdbcHttpSession
public class SessionControl {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
PlatformTransactionManager transactionManager;
private JdbcOperationsSessionRepository repository;
@RequestMapping(value="flpage", method=RequestMethod.GET)
public String showPage(Model model) {
repository = new JdbcOperationsSessionRepository(jdbcTemplate, transactionManager);
repository.setTableName("test.spring_session");
repository.setDefaultMaxInactiveInterval(120);
JdbcSession session = repository.createSession();
session.setAttribute("ATTR_USER", "rwinch");
repository.save(session);
return "flpage";
}
}
它告诉我导入 org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession;
不可见 那么我怎样才能正确使用内部 class 设置属性方法?我真的卡在这里了。
提前致谢,如有任何意见,我们将不胜感激。
首先,您应该使用接口(SessionRepository
或 FindByIndexNameSessionRepository
)与您的存储库交互并注入由 Spring 会话配置创建并注册为应用程序上下文中的一个 bean,而不是自己实例化 JdbcOperationsSessionRepository
。
基本上有两种方法可以注入 FindByIndexNameSessionRepository
实例 - 注入并使用原始类型,或参数化(尊重 FindByIndexNameSessionRepository<S extends Session>
的原始契约)。
原始类型方法:
class RawConsumer {
@Autowired
private FindByIndexNameSessionRepository sessionRepository;
void consume() {
Session session = (Session) this.sessionRepository.createSession();
session.setAttribute("test", UUID.randomUUID().toString());
this.sessionRepository.save(session);
}
}
参数化类型方法:
class ParameterizedConsumer<S extends Session> {
@Autowired
private FindByIndexNameSessionRepository<S> sessionRepository;
void consume() {
S session = this.sessionRepository.createSession();
session.setAttribute("test", UUID.randomUUID().toString());
this.sessionRepository.save(session);
}
}