I usually see in other codes which use final key word to inject Service Bean to Controllers, some code are not. So, what's the reason to use "final" in this. I know final variable that means it's immutable variable. I research some blogs that they say it relates to thread-safe.
Asked
Active
Viewed 923 times
1 Answers
3
Use final when using constructor injection:
constructor injection lets the riskAssessor field be marked as final, indicating that it cannot be subsequently changed
user7294900
- 52,490
- 20
- 92
- 189
-
But if I don't use final and use @Autowired in constructor and it still works normally? – Tam Huynh Thanh Dec 07 '20 at 02:15
-
@TamHuynhThanh if you use spring bean default singletone scope and write thread safe code then it will be thread safe, you don't have to mark as final – user7294900 Dec 07 '20 at 06:52