Inject request scoped bean to another bean

I want to create a UUID that is unique in the request life cycle. To do this, I create a UUID bean with the @Scope annotation ("request").

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

I want to access this bean in my controller. So I enter it using @Autowired. It works great.

@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";
    }
}

The controller calls several services. Each service uses a RestTemplate bean. In this RestTemplate bean, I want to get the 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);
    }
}

When I try to enter the UUID here, I have an error:

bean "zenithRestTemplate": ; - org.springframework.beans.factory.BeanCreationException: : private java.util.UUID com.geodis.rt.zenith.framework.webui.service.ZenithRestTemplate.uuid; - org.springframework.beans.factory.BeanCreationException: bean 'requestUUID': Scope 'request' ; - bean, ; - java.lang.IllegalStateException: : - ? - , , , DispatcherServlet/DispatcherPortlet: RequestContextListener RequestContextFilter, .

UUID bean RestTemplate bean?

Spring -MVC, Spring -boot java.

RequestContextListener, .

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

, UUID bean :

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

singleton bean, request scoped bean . singleton beans , scoped beans , .

Edit:

, @EnableAspectJAutoProxy .

+8

Source: https://habr.com/ru/post/1618603/


All Articles