Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring - Autowire using Setter - Annotations - XML

So, far we have seen Autowiring using @Autowired annotation. In setter injection using Autowiring we just have to put the @Autowired annotation above the setter method of the 'Car' class and everything remains the same.


@Autowired
public void setWheel(Wheel wheel) {
  this.wheel = wheel;


Wheel Class

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Wheel {

 @Value("Solid Tire")
 String tire;

 public String getTire() {
   return tire;
 }

 public void setTire(String tire) {
   this.tire = tire;
 }
}

Since, we have marked the 'Wheel' class with '@Component', Spring creates a 'Wheel' bean and with '@Value("Solid Tire")' it assignes a value 'Solid Tire' to the 'tire' property of 'Wheel'.



Car Class

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("car")
public class Car {


 Wheel wheel;

 public Wheel getWheel() {
   return wheel;
 }

 @Autowired
 public void setWheel(Wheel wheel) {
   this.wheel = wheel;
}

With '@Component("car")', Spring creates a 'Car' bean with name 'car'.


Now, with '@Autowired' annotation Spring searches for a bean of 'Wheel' type. And since the 'Wheel' bean is already there, Spring injects the 'Wheel' bean to the 'Car' class.


Thats all you need to do to get the Autowiring done.


Note : This time we have placed the @Autowired annotation above the setter method and not above 'Wheel wheel'.

Finally, we can write the application-config.xml


application-context.xml without <bean ..> tag

<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:annotation-config/>

  <context:component-scan base-package="com.learnerstutorial" />


</beans>

In the above xml we have added a few more namespaces for supporting annotation. You can copy the same and paste in your 'application-context.xml'.


Next, we are telling spring that the application has annotations.


<context:annotation-config/>

Also, inform spring that in which package it needs to scan for the annotated classes.


<context:component-scan base-package="com.learnerssight" />

Assuming our annotated classes are in 'com.learnerssight' package.


Finally, in the main() method we define our application code to interact with spring.


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MySpringApp {
  public static void main(String[] args) {

  ApplicationContext applicationContext =
            new ClassPathXmlApplicationContext("application-context.xml");

  Car car = (Car)applicationContext.getBean("car");

  System.out.println("The Car tire is : "+car.getWheel().getTire());

  }
}

Output :


 The Car tire is : Solid Tire