Spring Request scope vs java thread-local

前端 未结 3 792
轮回少年
轮回少年 2021-01-31 04:57

In high volume (~50,000 requests per second) java web-app I\'m using ThreadLocal to execute a task which should be executed per request scope.

I could achieve the same e

3条回答
  •  故里飘歌
    2021-01-31 05:24

    If we consider the traditional Java approach the answer can be deducted from the quote bellow as being much slower:

    Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

    Quoted from the JavaDoc about reflection - http://java.sun.com/docs/books/tutorial/reflect/index.html

    So since Spring uses Reflection with the getBean() method the SpringContext.getBean(SomeClass.class); approach should be slower.

    EDIT:

    Also note that the ThreadLocal also has caching embedded so as long as you reuse information in those threads it's for sure faster.

提交回复
热议问题