You are on page 1of 4

Spring Dependency Injection :-

Service.java :-
package com.arrams.spring.interfaces;
public interface Service {
public String getDependencyName();
}
ConstructorInjectionBean.java :-
package com.arrams.spring.bean;
import com.arrams.spring.interfaces.Service;
public class ConstructorInjectionean implements Service{
private String name;
public ConstructorInjectionean() {
}
public ConstructorInjectionean(String name) {
tis.name ! name;
}
public String getDependencyName() {
return name;
}
}
SetterInjectionBean.java :-
package com.arrams.spring.bean;
import com.arrams.spring.interfaces.Service;
public class SetterInjectionean implements Service{
private String name;
public SetterInjectionean() {
}
public void setName(String name) {
tis.name ! name;
}
public String getDependencyName() {
return name;
}
}
applicationConte!t.java :-
"#!ml version$"1.0" encoding$"UTF-8"#%
"beans
#m$ns!"http://www.springframework.org/schema/beans"
#m$ns%#si!"http://www.w3.org/2001/XMLSchema-instance"
#m$ns%p!"http://www.springframework.org/schema/p"
#si%sc&ema'ocation!"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.s!"(
"bean id!"constr"ctor"
c$ass!"com.arrams.spring.bean.#onstr"ctor$n%ection&ean"(
"constructor)arg va$ue!"sai"("*constructor)arg(
"*bean(
"bean id!"setter" c$ass!"com.arrams.spring.bean.Setter$n%ection&ean"(
"property name!"name" va$ue!"ragha'"("*property(
"*bean(
"*beans(
ConstructorInjectionBean&est.java :-
package com.arrams.spring.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframe+or,.beans.factory.#m$. -m$ean.actory ;
import org.springframe+or,.core.io.C$ass/at&0esource;
import com.arrams.spring.interfaces.Service;
public class Constructorean1est {
public static void main(String23 args) {
ean.actory factory ! ne' -m$ean.actory( ne'
C$ass/at&0esource( 4*com*arrams*spring*config*app$icationConte#t.#m$4 )) ;
Service service ! (Service) factory.getean(4constructor4);
System.o"t.print$n(service.getDependencyName());
}
}
If you are running t&e above java program first$y t&e container is started
because +e supp$ied a$$ t&e spring beans in an #m$ fi$e. Ne#t +e are trying to
ca$$ getean() met&od so t&e container +i$$ create t&e bean object and return
t&e object so t&at you can ca$$ t&e services t&at are provided by t&e object.
So in t&e above case +e get t&e output as %)
5*/%) sai
Setter(etodInjection&est.java :-
package com.arrams.spring.test;
import org.springframe+or,.beans.factory.ean.actory;
import org.springframe+or,.beans.factory.#m$. -m$ean.actory ;
import org.springframe+or,.core.io.C$ass/at&0esource;
import com.arrams.spring.interfaces.Service;
public class Setterean1est {
public static void main(String23 args) {
ean.actory factory ! ne' -m$ean.actory( ne'
C$ass/at&0esource( 4*com*arrams*spring*config*app$icationConte#t.#m$4 )) ;
Service service ! (Service) factory.getean(4setter4);
System.o"t.print$n(service.getDependencyName());
}
}
If you e#ecute t&e above program you +i$$ get t&e output as %)
)*p :- rag&av

You might also like