Zum Inhalt springen

@Autowired in Spring

Autowiring:
Autowiring means wiring the dependency in the particular class by SpringFramework. We give metadata configuration file to the IOC Container, based on the configuration IOC container creates the bean and inject the depenedency. It is achieved by using @Autowired annotation.

Autowiring is done in 3 ways:
1.Autowiring by Type

  1. Autowiring by Name
    3.Autowiring by Constructor

Note:
When there are multiple beans in same type for autowiring, there will be ambiguity. It will throw NoUniqueBeanDefinitionException.
If we missed to provide @Autowire on top of other class reference, we will get NullpointerException.

1.Autowiring by Type:
It is achieved through qualifier annotation

  1. Autowiring by Name:
    IOC container will inject the dependency when the instance variable and bean name are same.
    So we have to create our own bean name @Component(“shape”) – but it is not recommended.
    Otherwise we can have the bean name as reference name

3.Autowiring by constructor:
create reference for the class and create constructor using reference variable.
Add @qualifier(“bean”) inside constructor parameter

Advantage of Autowiring: It requires less code because we don’t need to write the code to inject the dependency explicitly.
Disadvantage of Autowiring: No control of the programmer and It can’t be used for primitive and string values.

The class is not annotated with @component we get NosuchbeanException

Icourse.java – Interface
package com.shristi.autowire;

public interface Icourse {
void show_courses();
}

CourseFactory.java
package com.shristi.autowire;

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

@Component
public class CourseFactory {
@Autowired //autowire by type
@Qualifier(„frontEnd“)
private Icourse course;

@Autowired //autowire by name
private Icourse backEnd;

//autowire by constructor
private Icourse cloud;


public CourseFactory(@Qualifier("cloud")Icourse cloud) {
    super();
    this.cloud = cloud;
}


public void printcourses() {
    course.show_courses();
    backEnd.show_courses();
    cloud.show_courses();
}

}

FrontEnd.java
package com.shristi.autowire;

import org.springframework.stereotype.Component;

@Component
public class FrontEnd implements Icourse{

@Override
public void show_courses() {
    System.out.println("HTML,CSS and JS");

}

}

Cloud.java
package com.shristi.autowire;

import org.springframework.stereotype.Component;

@Component
public class Cloud implements Icourse{

@Override
public void show_courses() {
    // TODO Auto-generated method stub
    System.out.println("AWS");
}

}

Backend.Java
package com.shristi.autowire;

import org.springframework.stereotype.Component;

@Component
public class BackEnd implements Icourse{

@Override
public void show_courses() {
    // TODO Auto-generated method stub
    System.out.println("JAVA and Springboot");
}

}

Automain.java

package com.shristi.autowire;

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

public class Automain {

public static void main(String[] args) {

    ApplicationContext context=new AnnotationConfigApplicationContext("com.shristi.autowire");
    CourseFactory factory=context.getBean(CourseFactory.class);
    factory.printcourses();
}

}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert