创建:
@Bean
@Lazy
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public SsoUser ssoUser() {
//TODO: find auth info from RPC context, http header, cookie, and validate it
return new SsoUser();
}
使用:
@Service
public static class ResourceService{
@Resource
SsoUser ssoUser;
public void listResources(){
if(ssoUser!=null).....
}
}
注意:
如果在第一步中不设置proxyMode,在启动时会报异常,比如’Scope ‘request’ is not active for the current thread’。因为这个参数的默认值是不代理,然而在上面的例子中,ResourceService在启动时只会注入SsoUser 这个bean一次,而由于ssoUser这个bean是request作用域,注入就会失败。可以参考这个文章的解释:csdn blog 79121887