据我测试,它正在工作。但我不明白它为什么以及如何工作。(而且我不确定它在生产中使用是否安全)
这是我的测试代码
@Service
public class SomeService {
private static final Logger logger = LoggerFactory.getLogger(SomeService.class);
private final RequestContext requestContext; // @RequestScope + @Component Bean
public SomeService(RequestContext requestContext) {
this.requestContext = requestContext;
}
public void checkBean() {
logger.info("Singleton Bean: {}, RequestScope Bean: {}", this, this.requestContext);
String clientId = recommendContext.getClientId();
}
}
Run Code Online (Sandbox Code Playgroud)
场景如下
SomeService由控制器注入RequestContextBeansomeService.checkBean()我觉得奇怪的一点是
SomeService是一个单例 beanRequestContext被声明为final变量并且仅由构造函数启动运行代码的结果如下所示
2021-06-14 09:56:26.010 INFO 23075 --- [nio-8888-exec-1] p.service.SomeService : Singleton Bean: pkgs.service.SomeServiceImpl@3c65ee26, RequestScope …Run Code Online (Sandbox Code Playgroud) 是否有可能在单个请求中使用线程静态变量?当前代码使用线程静态变量进行日志记录,现在我们希望使用异步控制器方法(使用async和await模式),这会导致问题,因为在打开新线程时该变量为null.
asp.net threadstatic async-await asp.net-web-api requestscope
我正在使用 Java8/Spring Boot 2 应用程序。我想将请求范围的 bean 注入到单例 bean 中。在官方文件,要么代理或的ObjectFactory /供应商应使用亮点保证一直在单身豆获得正确范围的bean在运行时。但是,@RequestScope 注释似乎“自动”设置了某种代理,如此问题的答案中所述。
我现在想知道以下三个实现是否实际上相同,哪个更受欢迎?
方法 1:显式使用 objectFactory<>
@Component
@RequestScope
public class MyRequestScopedBean{...}
@Component
public class MySingletonBean{
@Autowired
private ObjectFactory<MyRequestScopedBean> myRequestScopedBean
}
Run Code Online (Sandbox Code Playgroud)
方法2:正常注入,假设请求范围的bean是自动代理的?
@Component
@RequestScope
public class MyRequestScopedBean{...}
@Component
public class MySingletonBean{
@Autowired
private MyRequestScopedBean myRequestScopedBean
}
Run Code Online (Sandbox Code Playgroud)
方法 3:使用 @Configuration 和 @Bean 因为我不知道它们的区别,而且我担心它们的行为不同。
@Comfiguration
public class myBeanConfig{
@Bean
@RequestScope
public MyRequestScopedBean getRequestScopedBean(){return new MyRequestScopedBean();}
}
@Component
public class MySingletonBean{
@Autowired
private MyRequestScopedBean myRequestScopedBean
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢方法 2,因为它简洁并自动处理范围/代理。
如果我的@Autowired bean 被声明为一个 …
我正在尝试创建一个本地化的 JSF Web 应用程序,它允许用户通过下拉菜单选择一种语言。选择语言后,我模拟重定向到同一页面,但带有 URL 参数:
window.location.replace(urlToMyApp + '?locale=DE');
Run Code Online (Sandbox Code Playgroud)
接下来,我在应用程序的 Web 过滤器中读取“locale”参数并将其写入同名的 cookie 中:
String localeValue = httpRequest.getParameter("locale");
Cookie cookie = new Cookie("locale", localeValue);
cookie.setMaxAge(-1);
cookie.setDomain(cookieDomain);
cookie.setPath(cookiePath);
httpResponse.addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试在请求 bean init 方法中读取该 cookie 时,cookie 不可用。如果我通过下拉菜单(例如 EN)选择另一种语言,则在 init 方法中读取先前选择的语言 (DE)。
我假设在下一个“请求 - 响应”周期之前写入过滤器的 cookie 不可用,有人可以确认吗?
如果这是真的,我想在选择另一种语言后立即翻译我的应用程序。
我认为我需要提及一件事 - 语言下拉列表不是我的应用程序的一部分。它是包含多个应用程序的某种框架的一部分(如门户)。
是否有将 @RequestScope 与 Webflux 一起使用的模式?我们使用了此处建议的方法(https://www.baeldung.com/spring-bean-scopes),但它给出了以下错误。
没有为范围名称请求注册范围
requestscope ×5
spring-boot ×2
asp.net ×1
async-await ×1
cookies ×1
java ×1
jsf ×1
localization ×1
scope ×1
spring-bean ×1
threadstatic ×1