My program is using Spring Session xml based configuration and I using JdbcOperationsSessionRepository to implement my session. The JdbcOperationsSessionRepository library is using JdbcOperationsSessionRepository.JdbcSession and how can I set the session attributes?
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";
}
}
It show me import org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession;
is not visible so how can I use the inner class set attribute method properly? I really stuck here.
Thanks in advance, any comment is appreciate.
First off, you should use an interface (either SessionRepository
or FindByIndexNameSessionRepository
) to interact with your repository and inject the instance that was created by Spring Session configuration and registered as a bean in the application context, rather that instantiating JdbcOperationsSessionRepository
yourself.
There are basically two ways you can inject a FindByIndexNameSessionRepository
instance - either to inject and use raw type, or parameterized (respecting the original contract of FindByIndexNameSessionRepository<S extends Session>
).
Raw type approach:
class RawConsumer {
@Autowired
private FindByIndexNameSessionRepository sessionRepository;
void consume() {
Session session = (Session) this.sessionRepository.createSession();
session.setAttribute("test", UUID.randomUUID().toString());
this.sessionRepository.save(session);
}
}
Parameterized type approach:
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);
}
}