Autowiring in Spring
In a large application, all of explicit constructor or property wiring will make XML lengthy and hard to debug if anything needs to change in between..
Instead of explicitly wiring all of
your bean’s properties, you can use Spring autowire features to wire the dependencies for you. Spring provides the following four types of autowiring:-
- byName—Attempts to find a bean in the container whose name (or ID) is
the same as the name of the property being wired. If a matching bean is not
found, the property will remain unwired. - byType—Attempts to find a single bean in the container whose type
matches the type of the property being wired. If no matching bean is found,
the property will not be wired. If more than one bean matches, an
org.springframework.beans.factory.UnsatisfiedDependencyException
will be thrown. - constructor—Tries to match up one or more beans in the container with
the parameters of one of the constructors of the bean being wired. In the
event of ambiguous beans or ambiguous constructors, an org.springframework.beans.factory.UnsatisfiedDependencyException will be thrown. - autodetect—Attempts to autowire by constructor first and then using
byType. Ambiguity is handled the same way as with constructor and
byType wiring.
Example to Demonstrate byName and byType Autowiring :-
Note:-We have created one more xml file spring-beans1.xml for the Autowiring examples.
<?xml version="1.0" encoding="UTF-8"?> <property name="prices"> <bean id="setterInjectionBean" class="com.visualbuilder.beans.SetterInjectionBean" > </bean> |
AutoDetectExample.java
package com.visualbuilder.example; import org.springframework.context.ApplicationContext; import com.visualbuilder.beans.WrapperBean; public class AutoDetectExample { public static void main(String[] ar) { } |
Output:-
Jun 8, 2008 10:11:33 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Jun 08 10:11:33 IST 2008]; root of context hierarchy
Jun 8, 2008 10:11:34 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans1.xml]
Jun 8, 2008 10:11:34 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9
Jun 8, 2008 10:11:34 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9: defining beans [test,disposableInitBean,collectionInjectionBean,typeCollectionInjectionBean,constructorInjectionBean,setterInjectionBean,wrapperBeanByName,wrapperBeanByType,wrapperBeanAuto,wrapperBeanConstructor]; root of factory hierarchy
afterPropertiesSet method is called
Geting the Bean Autowired byName
Assigned Name is John Smith
Assigned Age is 25
Assigned Sex is M
Geting the Bean Autowired byType
Assigned Name is John Smith
Assigned Age is 25
Assigned Sex is M
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin




