to run this synchronizeOnSession example:
xxx
will wait for sleep
wake up!or test the synchronizeOnSession = false
xxx
will NOT wait for sleep
wake up!DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@Configuration
@ImportResource("servlet-context.xml")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
HelloWorldController.java
package com.example.demo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.tomcat.util.http.parser.Cookie;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* * Spring Boot HelloWorld. synchronizeOnSession example
*/
@RestController
public class HelloWorldController {
@RequestMapping(value = "/{param}")
public String sayHello(@PathVariable String param, HttpServletRequest request, HttpSession session) {
System.out.println("param=" + param);
Object id = session.getId();
if (id == null) {
System.out.println("session not exist " + id);
//session.setAttribute("p", p);
} else {
System.out.println("session existed ,id=" + id);
if (param.equals("sleep")) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("wake up!");
}
}
return "Hello,World! " + param;
}
}
MyPostProcessor.java
package com.example.demo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
@Component
public class MyPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
if (bean instanceof RequestMappingHandlerAdapter) {
RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
adapter.setSynchronizeOnSession(true);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
servlet-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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:property-placeholder
location="classpath:*.properties" />
<context:component-scan
base-package="com.example.demo.MyPostProcessor" />
</beans>
details ref: https://github.com/siumennel/demo https://dzone.com/articles/creating-a-spring-boot-project-with-eclipse-and-ma