我有一个控制器,我希望每个会话都是唯一的.根据spring文档,实现有两个细节:
1.初始Web配置
为了在请求,会话和全局会话级别(Web范围的bean)支持bean的范围,在定义bean之前需要一些小的初始配置.
我web.xml在文档中显示了以下内容:
<listener>
  <listener-class>
    org.springframework.web.context.request.RequestContextListener
  </listener-class>
</listener>
2. Scoped bean作为依赖项
如果要将(例如)HTTP请求作用域bean注入另一个bean,则必须注入AOP代理来代替作用域bean.
我@Scope提供了proxyMode如下所示的bean注释:
@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class ReportBuilder implements Serializable {
    ...
    ...
}
问题
尽管有上述配置,但我得到以下异常:
org.springframework.beans.factory.BeanCreationException:创建名为'scopedTarget.reportBuilder'的bean时出错:当前线程的作用域'session'不活动; 考虑为这个bean定义一个范围代理,如果你想从一个单例引用它; 嵌套异常是java.lang.IllegalStateException:找不到线程绑定请求:您是指在实际Web请求之外的请求属性,还是在最初接收线程之外处理请求?如果您实际在Web请求中操作并仍然收到此消息,则您的代码可能在DispatcherServlet/DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求.
更新1
下面是我的组件扫描.我有以下内容web.xml:
<context-param>
  <param-name>contextClass</param-name>
  <param-value>
    org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
</context-param>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>org.example.AppConfig</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
以下内容AppConfig.java:
@Configuration
@EnableAsync
@EnableCaching
@ComponentScan("org.example")
@ImportResource("classpath:applicationContext.xml")
public class AppConfig implements AsyncConfigurer {
  ...
  ...
}
更新2
我创建了一个可重现的测试用例.这是一个小得多的项目,因此存在差异,但会发生同样的错误.有相当多的文件,所以我上传它作为一个tar.gz以megafileupload.
在Spring MVC应用程序中,我有一个请求范围的bean.我在某处注入了这个bean.在那里,HTTP请求服务线程可能会产生一个新线程.
但每当我尝试从新生成的线程访问请求范围的bean时,我得到一个org.springframework.beans.factory.BeanCreationException(参见下面的堆栈跟踪).
从HTTP请求线程访问请求范围的bean工作正常.
如何为HTTP请求线程生成的线程提供请求范围的bean?
获取以下代码段运行.然后启动服务器,例如http://example.com:8080.
访问http://example.com:8080/scopetestnormal时,每次向此地址发出请求时,counter都会增加1(通过记录器输出会显着).:)超级!
访问http://example.com:8080/scopetestthread时,每次向此地址发出请求时,都会抛出上述异常.:(.无论什么选择ScopedProxyMode,这种情况发生两个基于CGLIB 和  
基于JDK动态代理接口请求范围豆
配置文件
package com.example.config
@Configuration
@ComponentScan(basePackages = { "com.example.scopetest" })
public class ScopeConfig {
    private Integer counter = new Integer(0);
    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Number counter() {
        counter = new Integer(counter.intValue() + 1);
        return counter;
    }
    /* Adding a org.springframework.social.facebook.api.Facebook request-scoped bean as a real-world example why all this matters
    @Bean
    @Scope(value = "request", …我有问题,我想将我的Spring WebMVC应用程序的一些进程外包到单独的Threads中.这很容易并且有效,直到我想使用一个使用全局请求的类userRightService.这在线程中不可用,我们遇到了一个问题,这几乎是可以理解的.
这是我的错误:
java.lang.RuntimeException:
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'scopedTarget.userRightsService': 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: Cannot ask for request attribute - 
request is not active anymore!
好的,够清楚了.我试图通过实现此解决方案来保持请求上下文:
这是我的runnable类:
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class myThread implements Runnable {
  private RequestAttributes context;
  public DataExportThread(RequestAttributes context) {
    this.context = context;
  }
  public …我精确地说,我是Java Developper一年级的法国学生。
我正在使用以下程序开发一个多模块应用程序:Spring Boot,Spring安全性,Hibernate,Spring Data,Spring MVC和Thymeleaf。
我想在登录时在会话中设置User,或者至少在userId中设置。这样,我不必每次需要时都将其手动放入会话或模型中。
但是当我使用默认的Spring Security登录和身份验证配置时,我真的不知道如何或在何处调用这种方法:
void putUserInHttpSession( HttpSession httpSession ) {
        httpSession.setAttribute( "user" , getManagerFactory().getUserManager().findByUserName( SecurityContextHolder.getContext().getAuthentication().getName()) );
    }
我可以在需要的时候做到这一点,但我发现很难做到不只是在登录时这样做!
这是我认为您可能需要帮助的内容(那真是太棒了!!! :)
我的WebSecurityConfig类:
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;
    @Autowired
    private DataSource dataSource;
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // Setting Service to find User in the database.
        // And Setting PassswordEncoder
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
    @Override
    protected void configure( HttpSecurity http ) …