Table of Contents
This is 10 of 16 parts of tutorial series
Tutorial Content: Spring tutorial for beginners
- Introduction to spring framework
- Spring interview questions
- Dependency injection(ioc) in spring
- Spring XML based configuration example
- Spring java based configuaration
- Dependency injection via setter method in spring
- Dependency injection via constructor in spring
- Spring Bean scopes with examples
- Initializing collections in spring
- Beans Autowiring in spring
- Inheritance in Spring
- Spring ApplicationContext
- Spring lifetime callbacks
- BeanPostProcessors in Spring
- Annotation based Configuration in spring
- Spring AOP tutorial
You have learnt how to declare beans using the element and inject with using and elements in XML configuration file.
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in .The Spring container can autowire relationships between collaborating beans without using and elements which helps cut down on the amount of XML configuration
1 2 3 |
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country" autowire="byName"> |
Autowiring modes:
no:
byName:
byType:
contructor:
autodetect:
Example:
1.Country.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.javapostsforlearning; public class Country { String countryName; Capital capital; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public Capital getCapital() { return capital; } public void setCapital(Capital capital) { this.capital = capital; } } |
2.Capital.java
Create Capital.java under package org.arpit.javapostsforlearning.java.Above Country class contains object of this class.Copy following content into Capital.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.javapostsforlearning; public class Capital { String capitalName; public String getCapitalName() { return capitalName; } public void setCapitalName(String capitalName) { this.capitalName = capitalName; } } |
3.BeanAutowirngByNameMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.javapostsforlearning; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanAutowiringByNameMain{ public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("country"); String countryName=countryObj.getCountryName(); String capitalName=countryObj.getCapital().getCapitalName(); System.out.println(capitalName+" is capital of "+countryName); } } |
4.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="country" class="org.arpit.javapostsforlearning.Country" autowire="byName"> <property name="countryName" value="India"/> </bean> <bean id="capital" class="org.arpit.javapostsforlearning.Capital"> <property name="capitalName" value="Delhi" /> </bean> </beans> |
Here in ,we have used autowire attribute and set it to “byName”.So now spring container will match “capital” reference in Country class with id or name of other beans in XML configuration file. So here you can see we have bean with id as “capital”
5.Run it
1 2 3 |
Delhi is capital of India |
Limitations of Autowiring :
- OverRiding possibilities:You can still define dependencies using or tag which will always override autowiring.
- Primitive data type:you have to define primitive data type like String or Interger using or tag.You can not autowire them.
- Confusing Nature:If you have lot of dependency in program,then its very hard to find out using autowire attribute of bean.
Source code:
1 2 3 4 |
<b>Source:</b><a href="https://dl.dropbox.com/s/nhd4teddllrdj0a/BeansAutowiringExample.7z" target="_blank">Download without jars files</a> <b>Source + lib</b>: <a href="https://dl.dropbox.com/s/lvrg5clnde9zmz8/BeansAutowiringExampleWithJars.7z" target="_blank">Download with jars files</a> |