Table of Contents
This is 7 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
As the name implies, using constructor spring container will inject the dependencies.
For configuring spring in your eclipse ide please refer hello world example
Dependency Injection via Constructor
1.Country.java:
We will create a simple class which has countryName and capital as attributes.
Create Country.java under package org.arpit.java2blog.Copy following content into Country.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; public class Country { String countryName; Capital capital; public Country(String countryName, Capital capital) { super(); this.countryName = countryName; this.capital = capital; } public String getCountryName() { return countryName; } public Capital getCapital() { return capital; } } |
2.Capital.java
Create Capital.java under package org.arpit.java2blog.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.java2blog; public class Capital { String capitalName; public String getCapitalName() { return capitalName; } public void setCapitalName(String capitalName) { this.capitalName = capitalName; } } |
3.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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="CountryBean" class="org.arpit.java2blog.Country"> <constructor-arg index="0" type="java.lang.String" value="India" /> <constructor-arg index="1" ref="CaptialBean" /> </bean> <bean id="CaptialBean" class="org.arpit.java2blog.Capital"> <property name="capitalName" value="Delhi" /> </bean> </beans> |
1.Class Country with id as “CountryBean”
2.Class Capital with id as “CapitalBean”
constructor-arg tag is used for providing argument to bean’ s constructor.type is for declaring data types and index defines position in constructor’s argument.
In above xml,Two arguments are passed.
1. India as string
2.CapitalBean ‘s reference
Property’s value tag is for assigning value to corresponding attribute. so In above xml file,we have assigned capitalName attribute of Capital class with value as Delhi
1 2 3 |
<property name="Name Of Attribute" value="Value Of attribute to be assigned"/> |
1 2 3 |
<property name="Name Of Attribute" value="id of referencing bean"/> |
4.ConstructorDIMain.java
This class contains main function.Create ConstructorDIMain.java under package org.arpit.java2blog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; public class ConstructorDIMain{ public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("CountryBean"); String countryName=countryObj.getCountryName(); String capitalName=countryObj.getCapital().getCapitalName(); System.out.println(capitalName+" is capital of "+countryName); } } |
You can note here that we have used ClassPathXmlApplicationContext for getting bean here.There are various ways for getting beans.In hello world example we have used XmlBeanFactory for getting beans.
5.Run it
When you will run above application, you will get following as output.
1 2 3 |
Delhi is capital of India |
In next post,we will see spring bean scopes
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.