우선 필자는 Spring 4.3.4 RELEASE 버전을 사용하며 자바 1.8 버전을 사용하고 있다.
// pom.xml
<!-- redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
pom.xml 에 위의 maven 저장소를 추가한다.
그리고 redis 서버와 연동하기 위해 필자는 [/WEB-INF/spring/xml] 이란 폴더에 [redis-context.xml] 이란 파일을 따로 생성하였다.
// redis-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />
<bean
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:port="6379" p:hostName="localhost" />
</beans>
포트랑 hostname 은 각자의 사정에 맞추자~ 융통성 있게~
이제 web.xml 에서 해당 세션을 redis 세션으로 필터링 해주도록 설정을 해야한다. spring boot 에선 알아서 해줬는데;
해당 filter 부분만 올리면 헷갈리는 사람도 있으니까 web.xml 전체 코드를 올리겠다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/xml/redis-context.xml <!-- 추가 -->
</param-value>
</context-param>
//////////////
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
///////////////
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
이제 컨트롤러에서 테스트 코드를 작성해보장~
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(HttpSession session, Locale locale, Model model) {
if(session.getAttribute("test") == null){
session.setAttribute("test", "hi");
}else{
System.out.println(session.getAttribute("test"));
}
return "home";
}
이런 식으로 하면 스프링 서버 껐다가 켜도 redis server 는 살아있기 때문에 hi 가 계속 출력되는 것을 볼 수 있을 것이다. 수고링~
수고링~
수고링~
수고링~
반응형