【发布时间】:2018-11-21 12:31:01
【问题描述】:
package com.elsoproject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("session")
public class SpyGirl {
public String iSaySomething() {
return "spicy vagyok";
}
}
package com.elsoproject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@Autowired
private SpyGirl spicey;
@RequestMapping("/")
public String index() {
return spicey.iSaySomething();
}
}
例外:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'spicey'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'spyGirl': Scope 'session' 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
【问题讨论】:
-
问题是什么?
-
你用的是spring-boot吗?
-
如果您提出具体问题并仔细格式化您的问题以便它们实际上是可读的,您可能会发现它更容易
标签: java spring spring-boot