The term auto itself says half the meaning of this wiring, that the things are done implicitly without the hard-coding any thing. Wiring defines the process of injection of the dependencies of the bean. So in Auto Wiring we are not hard-coding the process by which Container will be injecting the dependencies. We are just indicating the container to do the injection by writing something in the xml file using " autowire " attribute. Autowire is an attribute of the < bean > tag and has four different values. We have four different ways by which we can wire the dependencies and containers have four different ways to inject the dependencies. They are :
1. byName2. byType
3. constructor
4. autodetect ( Not in Spring 3.0 )
byName and byType ---> Setter Injection.
constructor ---> Constructor Injection.
autodetect ---> Setter or Constructor Injection.
Syntax for using autowire :
<bean id=" " class=" " autowire="xxx" />
byName
In this type of auto-wiring the bean should be detected by the container using the name of the reference of the dependent bean, which must be hard-corded with its bean definition in the " id " attribute.
Its not a good idea because its again hard-coding the name and there may chance of human error.
More about byName with examples
byType
In this type of auto-wiring the bean should be detected by the container using the declared reference type of the dependent bean.
Its better than byName type auto-wiring.
More about byType with examples
Constructor
In this type of auto-wiring the bean should be detected by the container matching the type of the instance running in the container with the data type mentioned in the dependent bean with the reference.
More about constructor with examples
Autodetect
It is a type of auto-wiring in which there may be chance of injecting the dependency either by constructor or by setter method. All depends on whether any constructor is defined in the dependent bean. If yes then inject through constructor otherwise through setter method.So it gives first priority to the constructor injection.
More about Autodetect with examples
ReplyDeletenice article for beginners.thank you.
java tutorial
spring tutorial