9

As per documentation it has been mentioned

it is by design

I want to understand the possible thinking behind this design.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • 1
    Injected beans need to have a class, a primitive type doesn't have one. However, using auto unboxing, feel free to inject them as their Object counterparts : Boolean, Integer, Double.. – Arnaud Dec 30 '15 at 10:18

4 Answers4

8

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.

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
1

Autowiring can't be used to inject primitive and string values. It works with reference only. Injected referance need to have a class which is object. you can use wrapper for same. i.e. Interger, Boolean.

For primitive you can do like spring-bean-primitive-properties-when-using-component-and-autowired

Community
  • 1
  • 1
bNd
  • 7,512
  • 7
  • 39
  • 72
0

Instead of autowiring a primitive type, you can autowire the wrapper class for the int type --> use Integer.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
0

Autowiring is just an alternative for referencing your existing beans in ApplicationContext.

primitives are not as bean which are existing in the application context

it expects an instance/bean not a primitive.

Darshan
  • 409
  • 4
  • 13