Spring 5 -Dependency Injection.
Tags: webdev
Published: 2023-01-13 12:03:19


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlahhcfbxux4cuxau65x.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j3erb4kp09cdqkk426e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l08ak5u1yb0j0uf89v5x.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84vmf1c70xszq5i7wzje.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xymk00bwk2mvzv7n33t.png)


**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;
	}

```


     

```
``Coach theCoach = context.getBean("myCoach",Coach.class);

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

```
<bean id="myFortune" class = "com.spring5.IOC.FortuneServiceImpl"></bean>
	<bean id="myCoach" class="com.spring5.IOC.CricketCoach"><property name = "theFortuneService" ref ="myFortune"></property></bean>
```


`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;
	}
}`

`<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();
	}

```