DEV Community

Saravanan B
Saravanan B

Posted on • Updated on

Spring 5 -Dependency Injection.

Dependency Injection
The client delegates to calls to another object the responsibility of providing its dependencies.

There are many types of injection with spring.
most used is Setter and Constructor Injection.

Example :
if we have employee class the class has a tight coupling on address class.
to overcome tight coupling dependency injection is introduced.

Image description

Image description

Image description

Image description

Image description

Setter Injection

we will create a getter setter method.
Then in application.xml using the property field we will declare property and reference variable.

private FortuneServiceImpl theFortuneService;

    public void setTheFortuneService(FortuneServiceImpl theFortuneService) {
        System.out.println("Cricket : Inside setter methods");
        this.theFortuneService = theFortuneService;
    }

Enter fullscreen mode Exit fullscreen mode
``Coach theCoach = context.getBean("myCoach",Coach.class);

     System.out.println(theCoach.getDaily());

     System.out.println(theCoach.getDailyFortune());
Enter fullscreen mode Exit fullscreen mode
<bean id="myFortune" class = "com.spring5.IOC.FortuneServiceImpl"></bean>
    <bean id="myCoach" class="com.spring5.IOC.CricketCoach"><property name = "theFortuneService" ref ="myFortune"></property></bean>
Enter fullscreen mode Exit fullscreen mode

`public class CricketCoach implements Coach {
private FortuneServiceImpl theFortuneService;

public void setTheFortuneService(FortuneServiceImpl theFortuneService) {
    System.out.println("Cricket : Inside setter methods");
    this.theFortuneService = theFortuneService;
}

@Override
public String getDaily() {
    return "Practice Cricket in ground for 3hours";
}

public CricketCoach() {
    System.out.println("CricketCoach : Inside no arg constructor");
}

@Override
public String getDailyFortune() {
    return theFortuneService.getDailyFortune();
}

private String email;
private String team;

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    System.out.println("Cricket : Inside set email");
    this.email = email;
}

public String getTeam() {
    return team;
}

public void setTeam(String team) {
    System.out.println("Cricket : Inside set team");
    this.team = team;
}
Enter fullscreen mode Exit fullscreen mode

}`

<bean id="myFortune" class = "com.spring5.IOC.FortuneServiceImpl"></bean>
<bean id="myCoach" class="com.spring5.IOC.CricketCoach"><property name = "theFortuneService" ref ="myFortune"></property>
<property name="email" value="sunrises@hydrabed.com"></property>
<property name="team" value="suraises"></property></bean>

public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        // creating a bean
//      Coach theCoach = context.getBean("myCoach",Coach.class);
        CricketCoach theCoach = context.getBean("myCoach",CricketCoach.class);

//      EmployeeId emp = context.getBean("employee", EmployeeId.class);
        // printing the bean created
        System.out.println(theCoach.getDaily());
        System.out.println(theCoach.getDailyFortune());
        System.out.println(theCoach.getEmail());
        System.out.println(theCoach.getTeam());
        // closing the bean
        context.close();
    }

Enter fullscreen mode Exit fullscreen mode

Top comments (0)