Saturday 10 November 2012

Auto-Wiring in Spring

Auto-wiring is a way of resolving the beans that need to be injected by inspecting the elements in ApplicationContext. Auto-wiring reduces the configuration for the properties and constructor.If configuration is changed for the property bean than it automatically updated.There are 4 kind of auto-wiring :
1.No (Default).
2.byName.
3.byType.
4.Constructor.

In 'default' auto-wiring means there is no wiring and we are still using the ' ref ' keyword for connecting beans. In 'byName' auto-wiring Spring uses the property 'name' and searches a bean with same name as property and  wired it .In 'byType' auto-wiring Spring uses the property 'type' and searches beans with same type as our property and wired it.If there are more than one bean with same type than it will give us a error as :

" Error creating bean with name 'person' defined in class path resource [Dependency.xml]: Unsatisfied dependency expressed through bean property 'name': : No unique bean of type [studyMaterial.Name] is defined: expected single matching bean but found 2: [name1, name2];"

So when you are using 'byType' than there should be only one bean of that type.In the same way if we are using constructor and in our constructor we are using property than it can also be auto-wired by using '   'autowire ="constructor"'.Here is a simple picture for representing auto-wiring.

No auto-wiring:
Auto-wiring :

Here we can see by using 'autowire' we can reduce the configuration of our xml file. Person is a class having property 'name' which is a type of  'Name' , so Spring searches for 'Name' type and bean 'name1' is a Name type because we are implementing 'Name' interface in our 'studyMaterial.DisplayThisName' class.So it connect name1 with person implicitly.One more thing I like to share in this topic which is annotation based wiring, so lets have a look.


Annotation Based Bean-wiring: 

 

Till now for wiring we are using "autowire" attribute. Ok how It would be if didn't need that anymore in our xml file and Spring automatically wire your beans.sounds good......na, ya we can do it by using annotation in our application. Some things needed for configure the annotation.
1.We have to add some files such as :
  •  xmlns:context="http://www.springframework.org/schema/context" .
  •  http://www.springframework.org/schema/context .
  • http://www.springframework.org/schema/context/spring-context-2.5.xsd .
  •  <context:annotation-config/>
Here is our configured file for our person and name example,

                                                                                                 
Now we can see that we need no attribute for wiring it automatically searches for you beans and wired it.One more thing, in our Person class, just above the property declaration we have to put a "@Autowired" keyword just like as:


Now it is complete and you can see that still our program running successfully without using autowire keyword.So enjoy with annotations.


Your mail are always welcomed at java.ansh@gmail.com.


No comments:

Post a Comment