Spring container has capability of automatically injecting dependencies while initiating bean. There are 5 different modes.
- No Autowire - XML Based Default
- Autowire by Type - Java Based Default
- Autowire by Name
- Autowire by Constructor
- Autodetect - Deprecated
You can turn on autowire feature by using following code in your XML. With this you can start using @Autowire annotation in your Java file.
You can autowire class properties directly even if they are private, you can autowire fields by constructor parameters or setters parameter. What if you have two ApplicationUser object in container which one application object will use, In XML based you can just give object name but in Java class you will have to use @Qualifier(“applicationUser2”) to identify which one to pick.
What if container doesn’t find the matching object? It will throw error, you can suppress this by using @Autowired (required=false)