Pages

Monday, 29 July 2013

Autowiring in Spring

Wiring show the relationship between the bean & Spring Container. Using the Wiring you are tell
the Spring container which bean needed and how should that dependencies inject.In Spring we can
automatically wire using the autowire property of bean.


<bean id="student" class="com.piyushr.Student" autowire="autowire type" />

There are Five types of Autowire type

1). none - By Default autowire is not enable. You have enable using the autowire property of bean
2). byName - Autowire by the bean propery name. Finding the name of bean in container if it match with property
    name then wired it otherwise the property remain unwired.
3). byType - Autowire by the bean propery type. Finding the single bean in container whose type match with the
    property type.if not finding the match property then it will be unwired. if we finding the multiple bean
    with same property type then it throws the UnsatisfiedDependencyExcpetion .
4). Constructor - Tries to match one or more bean in container and match byType arguments.if there is ambiguous
    in the constructor then it's throw the UnsatisfiedDependencyExcpetion.
5). autodetect - First it try to find with the default constructor otherwise wiring byType.

By default autowire none. But for all the beans we can set the autowiring type like this

<beans default-autowire="byName" />

Let's see the Spring Autowiring Example by annotation

No comments:

Post a Comment