gpt4 book ai didi

java - 将请求范围的 bean 注入(inject)另一个 bean

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:46 24 4
gpt4 key购买 nike

我想创建一个在请求生命周期中唯一的 UUID。为此,我创建了一个带有 @Scope("request") 注释的 UUID bean。

@Bean
@Scope(scopeName = WebApplicationContext.SCOPE_REQUEST)
public UUID requestUUID() {
return UUID.randomUUID();
}

我想在我的 Controller 中访问这个 bean。所以我用@Autowired注入(inject)它。这很好用。

@Controller
public class DashboardController {

@Autowired
UUID uuid;

@Autowired
WelcomeMessageService welcomeMessageService;

@Autowired
IssueNotificationService issueNotificationService;

@RequestMapping("/")
public String index(Model model) throws InterruptedException, ExecutionException {
System.out.println(uuid);
PortalUserDetails userLog = getPortalUserDetails();

BusinessObjectCollection<WelcomeMessage> welcomeMessages = welcomeMessageService.findWelcomeMessages(
20,
0,
userLog.getZenithUser(),
userLog.getConnectionGroup().getConnectionGroupCode(),
"FR");
if(welcomeMessages!=null) {
model.addAttribute("welcomeMessages", welcomeMessages.getItems());
}

BusinessObjectCollection<IssueNotification> issueNotifications =
issueNotificationService.findIssueNotifications(userLog.getZenithUser());

if(welcomeMessages!=null) {
model.addAttribute("welcomeMessages", welcomeMessages.getItems());
}
model.addAttribute("issueNotifications", issueNotifications);

return "index";
}
}

Controller 调用多个服务。每个服务都使用一个 RestTemplate bean。在这个 RestTemplate bean 中,我想获取 UUID。

@Component
public class ZenithRestTemplate extends RestTemplate {
@Autowired
private UUID uuid;

public void buildRestTemplate() {
List restTemplateInterceptors = new ArrayList();
restTemplateInterceptors.add(new HeaderHttpRequestInterceptor("UUID", uuid.toString()));
this.setInterceptors(restTemplateInterceptors);
}
}

当我尝试在此处注入(inject) UUID 时,出现错误:

Error creating bean with name 'zenithRestTemplate': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.UUID com.geodis.rt.zenith.framework.webui.service.ZenithRestTemplate.uuid; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestUUID': 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

如何访问 RestTemplate bean 中的 UUID bean?

我的项目使用 Spring-MVC,Spring-boot 和 java 配置。

我已经尝试添加一个 RequestContextListener,但它并没有解决问题。

@Bean public RequestContextListener requestContextListener(){
return new RequestContextListener();
}

最佳答案

我认为您需要标记您的 UUID 请求作用域 bean,例如:

@Scope(scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)

Controller 是 singleton 作用域 bean,您正在其中注入(inject) request 作用域 bean。由于单例 bean 在其生命周期内仅注入(inject)一次,因此您需要提供作用域 bean 作为代理来处理这一点。

另一种选择是使用 org.springframework.web.context.annotation.RequestScope 注释:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope(WebApplicationContext.SCOPE_REQUEST)
public @interface RequestScope {

@AliasFor(annotation = Scope.class)
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;

}

@RequestScope@Scope 上的元注释,它 1) 将 scope 设置为 "request" 和 2) 将 proxyMode 设置为 ScopedProxyMode.TARGET_CLASS,这样您就不必在每次要定义请求作用域的 bean 时都这样做。

编辑:

请注意,您可能需要在主配置类上添加 @EnableAspectJAutoProxy

关于java - 将请求范围的 bean 注入(inject)另一个 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34090750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com