1

One of my classes deals with HttpServletRequest and is a component like this:

@Component
public class AuthorizationService {

    @Autowired
    HttpServletRequest request;

    public Boolean authorize(Integer fetId) {
       ...play with headers, db and other stuff...
    }

and is used somewhere else like this

public class CarRestController {
    @Autowired
    CarService service; 
    @Autowired
    AuthorizationService authorizer;

    @RequestMapping(method = RequestMethod.GET)
    public List<Car> get()throws Exception {
        authorizer.authorize(666);
        ...
        return cars;
    }

My worry is that since AuthorizationService is a @component, it will be a singleton by default and therefore there can only be one request that will be swapped by newer ones coming as it is processed.

Should I do this to solve the problem?

@Component
@Scope("prototype")
public class AuthorizationService {

Many Thanks

MikaelW
  • 1,145
  • 4
  • 11
  • 29

2 Answers2

0

Remove the @Scope on the service and don't worry, your controller is also a singleton (because it's managed by spring).

BTW: you are missing a @Controller on your controller

Some reading:

Community
  • 1
  • 1
0

Spring takes care of request scoped object like HttpServletRequest automatically. Remove @Scope and you should be fine.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71