Spring Autowire Request Scope












0















In Spring it's easy to autowire beans and have them available anywhere in the app context. Beans can be specialized to a scope such as session/request/web socket etc.



I have a rather unique scenario. I receive a message from a message broker which means the request is not received in a "Controller". Because of this, Spring is not creating @RequestScope beans (All of this logic in Spring is based on using the @Controller/@RequestMapping annotations / DispatchServlet handler).
Is there a way to create a bean within the request scope with the Spring AutowireCapableBeanFactory or some other way?



I want to do something like the below in which the SomeService.handle will be able to access the getName() method of the RequestScopeBean. Currently it throws this exception.



Exception:



BeanCreationException: Error creating bean with name ' 
scopedTarget.getRequestUtils': Scope 'request' is not active for the
current thread; consider defining a scoped proxy for this bean


Code



@Service
public class MyMessagingReceiver implements SomeMessageReceiver {

private final SomeService someService;

@Autowired
public MyMessagingReceiver(final SomeService someService) {
this.someService = someService;
}

public void onMessage(MessageObject messageObject) {
//possible here to use AutowireCapableBeanFactory in inject the RequestScopeBean bean?
someService.handle(messageObject);
}
}

@Service
public class SomeService {

private final RequestScopeBean requestScopeBean;

@Autowired
public SomeService(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

public void handle(MessageObject messageObject) {
System.out.println(this.requestScopeBean.getName());
}

}

@Configuration
public class BeanDeclarations {
@Bean
@RequestScope
public RequestScopeBean requestScopeBean() {
return new RequestScopeBean();
}
}

public RequestScopeBean {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public class Interceptor extends HandlerInterceptorAdapter {
private RequestScopeBean requestScopeBean;

@Autowired
public Interceptor(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String name = request.getHeader("name");
this.requestScopeBean.setName(name);
}


}









share|improve this question

























  • Why not annotate your message broker client with @Controller and get access to the context that way? I'm unclear what the reason is for opting not to do that.

    – sofend
    Nov 19 '18 at 23:32











  • Wow, let me actually try that, no idea why this didn't occur to me

    – GSUgambit
    Nov 20 '18 at 2:58













  • that did not work, probably because there is no @RequestMapping

    – GSUgambit
    Nov 20 '18 at 3:32











  • The answer to this one might help: stackoverflow.com/questions/15415688/…

    – moilejter
    Nov 20 '18 at 5:13











  • No you cannot. As request and session scopes are tied to the web (and not related to @Controller it is the web that is important here). There is no web notion for in incoming message so no that will not work.

    – M. Deinum
    Nov 20 '18 at 9:18
















0















In Spring it's easy to autowire beans and have them available anywhere in the app context. Beans can be specialized to a scope such as session/request/web socket etc.



I have a rather unique scenario. I receive a message from a message broker which means the request is not received in a "Controller". Because of this, Spring is not creating @RequestScope beans (All of this logic in Spring is based on using the @Controller/@RequestMapping annotations / DispatchServlet handler).
Is there a way to create a bean within the request scope with the Spring AutowireCapableBeanFactory or some other way?



I want to do something like the below in which the SomeService.handle will be able to access the getName() method of the RequestScopeBean. Currently it throws this exception.



Exception:



BeanCreationException: Error creating bean with name ' 
scopedTarget.getRequestUtils': Scope 'request' is not active for the
current thread; consider defining a scoped proxy for this bean


Code



@Service
public class MyMessagingReceiver implements SomeMessageReceiver {

private final SomeService someService;

@Autowired
public MyMessagingReceiver(final SomeService someService) {
this.someService = someService;
}

public void onMessage(MessageObject messageObject) {
//possible here to use AutowireCapableBeanFactory in inject the RequestScopeBean bean?
someService.handle(messageObject);
}
}

@Service
public class SomeService {

private final RequestScopeBean requestScopeBean;

@Autowired
public SomeService(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

public void handle(MessageObject messageObject) {
System.out.println(this.requestScopeBean.getName());
}

}

@Configuration
public class BeanDeclarations {
@Bean
@RequestScope
public RequestScopeBean requestScopeBean() {
return new RequestScopeBean();
}
}

public RequestScopeBean {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public class Interceptor extends HandlerInterceptorAdapter {
private RequestScopeBean requestScopeBean;

@Autowired
public Interceptor(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String name = request.getHeader("name");
this.requestScopeBean.setName(name);
}


}









share|improve this question

























  • Why not annotate your message broker client with @Controller and get access to the context that way? I'm unclear what the reason is for opting not to do that.

    – sofend
    Nov 19 '18 at 23:32











  • Wow, let me actually try that, no idea why this didn't occur to me

    – GSUgambit
    Nov 20 '18 at 2:58













  • that did not work, probably because there is no @RequestMapping

    – GSUgambit
    Nov 20 '18 at 3:32











  • The answer to this one might help: stackoverflow.com/questions/15415688/…

    – moilejter
    Nov 20 '18 at 5:13











  • No you cannot. As request and session scopes are tied to the web (and not related to @Controller it is the web that is important here). There is no web notion for in incoming message so no that will not work.

    – M. Deinum
    Nov 20 '18 at 9:18














0












0








0








In Spring it's easy to autowire beans and have them available anywhere in the app context. Beans can be specialized to a scope such as session/request/web socket etc.



I have a rather unique scenario. I receive a message from a message broker which means the request is not received in a "Controller". Because of this, Spring is not creating @RequestScope beans (All of this logic in Spring is based on using the @Controller/@RequestMapping annotations / DispatchServlet handler).
Is there a way to create a bean within the request scope with the Spring AutowireCapableBeanFactory or some other way?



I want to do something like the below in which the SomeService.handle will be able to access the getName() method of the RequestScopeBean. Currently it throws this exception.



Exception:



BeanCreationException: Error creating bean with name ' 
scopedTarget.getRequestUtils': Scope 'request' is not active for the
current thread; consider defining a scoped proxy for this bean


Code



@Service
public class MyMessagingReceiver implements SomeMessageReceiver {

private final SomeService someService;

@Autowired
public MyMessagingReceiver(final SomeService someService) {
this.someService = someService;
}

public void onMessage(MessageObject messageObject) {
//possible here to use AutowireCapableBeanFactory in inject the RequestScopeBean bean?
someService.handle(messageObject);
}
}

@Service
public class SomeService {

private final RequestScopeBean requestScopeBean;

@Autowired
public SomeService(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

public void handle(MessageObject messageObject) {
System.out.println(this.requestScopeBean.getName());
}

}

@Configuration
public class BeanDeclarations {
@Bean
@RequestScope
public RequestScopeBean requestScopeBean() {
return new RequestScopeBean();
}
}

public RequestScopeBean {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public class Interceptor extends HandlerInterceptorAdapter {
private RequestScopeBean requestScopeBean;

@Autowired
public Interceptor(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String name = request.getHeader("name");
this.requestScopeBean.setName(name);
}


}









share|improve this question
















In Spring it's easy to autowire beans and have them available anywhere in the app context. Beans can be specialized to a scope such as session/request/web socket etc.



I have a rather unique scenario. I receive a message from a message broker which means the request is not received in a "Controller". Because of this, Spring is not creating @RequestScope beans (All of this logic in Spring is based on using the @Controller/@RequestMapping annotations / DispatchServlet handler).
Is there a way to create a bean within the request scope with the Spring AutowireCapableBeanFactory or some other way?



I want to do something like the below in which the SomeService.handle will be able to access the getName() method of the RequestScopeBean. Currently it throws this exception.



Exception:



BeanCreationException: Error creating bean with name ' 
scopedTarget.getRequestUtils': Scope 'request' is not active for the
current thread; consider defining a scoped proxy for this bean


Code



@Service
public class MyMessagingReceiver implements SomeMessageReceiver {

private final SomeService someService;

@Autowired
public MyMessagingReceiver(final SomeService someService) {
this.someService = someService;
}

public void onMessage(MessageObject messageObject) {
//possible here to use AutowireCapableBeanFactory in inject the RequestScopeBean bean?
someService.handle(messageObject);
}
}

@Service
public class SomeService {

private final RequestScopeBean requestScopeBean;

@Autowired
public SomeService(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

public void handle(MessageObject messageObject) {
System.out.println(this.requestScopeBean.getName());
}

}

@Configuration
public class BeanDeclarations {
@Bean
@RequestScope
public RequestScopeBean requestScopeBean() {
return new RequestScopeBean();
}
}

public RequestScopeBean {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public class Interceptor extends HandlerInterceptorAdapter {
private RequestScopeBean requestScopeBean;

@Autowired
public Interceptor(RequestScopeBean requestScopeBean) {
this.requestScopeBean = requestScopeBean;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String name = request.getHeader("name");
this.requestScopeBean.setName(name);
}


}






java spring spring-boot autowired requestscope






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:17









M. Deinum

69.6k13140149




69.6k13140149










asked Nov 19 '18 at 23:02









GSUgambitGSUgambit

5332820




5332820













  • Why not annotate your message broker client with @Controller and get access to the context that way? I'm unclear what the reason is for opting not to do that.

    – sofend
    Nov 19 '18 at 23:32











  • Wow, let me actually try that, no idea why this didn't occur to me

    – GSUgambit
    Nov 20 '18 at 2:58













  • that did not work, probably because there is no @RequestMapping

    – GSUgambit
    Nov 20 '18 at 3:32











  • The answer to this one might help: stackoverflow.com/questions/15415688/…

    – moilejter
    Nov 20 '18 at 5:13











  • No you cannot. As request and session scopes are tied to the web (and not related to @Controller it is the web that is important here). There is no web notion for in incoming message so no that will not work.

    – M. Deinum
    Nov 20 '18 at 9:18



















  • Why not annotate your message broker client with @Controller and get access to the context that way? I'm unclear what the reason is for opting not to do that.

    – sofend
    Nov 19 '18 at 23:32











  • Wow, let me actually try that, no idea why this didn't occur to me

    – GSUgambit
    Nov 20 '18 at 2:58













  • that did not work, probably because there is no @RequestMapping

    – GSUgambit
    Nov 20 '18 at 3:32











  • The answer to this one might help: stackoverflow.com/questions/15415688/…

    – moilejter
    Nov 20 '18 at 5:13











  • No you cannot. As request and session scopes are tied to the web (and not related to @Controller it is the web that is important here). There is no web notion for in incoming message so no that will not work.

    – M. Deinum
    Nov 20 '18 at 9:18

















Why not annotate your message broker client with @Controller and get access to the context that way? I'm unclear what the reason is for opting not to do that.

– sofend
Nov 19 '18 at 23:32





Why not annotate your message broker client with @Controller and get access to the context that way? I'm unclear what the reason is for opting not to do that.

– sofend
Nov 19 '18 at 23:32













Wow, let me actually try that, no idea why this didn't occur to me

– GSUgambit
Nov 20 '18 at 2:58







Wow, let me actually try that, no idea why this didn't occur to me

– GSUgambit
Nov 20 '18 at 2:58















that did not work, probably because there is no @RequestMapping

– GSUgambit
Nov 20 '18 at 3:32





that did not work, probably because there is no @RequestMapping

– GSUgambit
Nov 20 '18 at 3:32













The answer to this one might help: stackoverflow.com/questions/15415688/…

– moilejter
Nov 20 '18 at 5:13





The answer to this one might help: stackoverflow.com/questions/15415688/…

– moilejter
Nov 20 '18 at 5:13













No you cannot. As request and session scopes are tied to the web (and not related to @Controller it is the web that is important here). There is no web notion for in incoming message so no that will not work.

– M. Deinum
Nov 20 '18 at 9:18





No you cannot. As request and session scopes are tied to the web (and not related to @Controller it is the web that is important here). There is no web notion for in incoming message so no that will not work.

– M. Deinum
Nov 20 '18 at 9:18












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383882%2fspring-autowire-request-scope%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383882%2fspring-autowire-request-scope%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings