Search code examples
javaspringdependency-injectionspring-ioc

Why Spring framework does not allow autowire primitive types?


As per documentation it has been mentioned

it is by design

I want to understand the possible thinking behind this design.


Solution

  • It's because Autowiring is just an alternative for referencing your existing beans in ApplicationContext. It expects a bean or a class, primitive is not a class and it differs from Object. Which is why you can't Autowire primitive types. You can use wrapper classes of the primitive types such as Integer, Double etc... to be able to use Autowiring for such types because you are now referring to a class.