Reputation: 31
I have a spring boot app, in the controller I have an injected service class, and there and one of the method from that service class called from a thread. My app has an index page where the user can set values and start some computing, setting values happens in that service class, to start computing, the user clicks on a button which will start the thread in the controller which calls a method in service class. Results will be shown in different page, this page there is a button which leads back to the index page to start ne computings with different values.
I need a new injected bean when I return to index page after a computing I guess, without that I will modify and use the previous one which will give me false results.
What scope I need for my service class?
I tried whith prototype, request and session, last two give me errors before runnning my code, first give me false results.
"Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'schedulerController': Unsatisfied dependency expressed through method 'setA' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request."
How to solve the problem?
In my controller:
@Autowired
public void setA(Scheduler schedulerObject) {
this.schedulerObject = schedulerObject;
}
static class thread extends Thread
{
public void run()
{
try {
schedulerObject.setRun(true);
schedulerObject.check();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void kill() {
schedulerObject.setRun(false);
}
}
This method in my controller will start computing:
static thread x;
@RequestMapping(value="start")
public String start() throws InterruptedException
{
x=new thread();
x.start();
Thread.sleep(1*1000);
return "index";
}
@RequestMapping(value="stop")
public String stop() throws InterruptedException
{
x.kill();
x.interrupt();
return "index";
}
There are other controller methods to do some modification in "schedulerObject" and the last methods called by user are "start" and "stop"
So every time the user goes back to the index page a new instance of "schedulerObject" is needed.
Upvotes: 2
Views: 2776
Reputation: 10972
You seem to be lacking a RequestContextListener
in your spring configuration:
@Bean
public RequestContextListener requestContextListener(){
return new RequestContextListener();
}
Without this bean you won't be able to create request scoped beans (see here).
Also related: Configuring RequestContextListener in SpringBoot
Upvotes: 4