'In spring boot what is the difference between importing other class and by using @autowired?

In spring boot in ClassA we can get ClassB methods by importing ClassB class,the same behaviour we are doing with the @autowired. So what is the major difference between these two.



Solution 1:[1]

They are completely different things. Using the import statement is not a Spring Boot thing, it is a Java thing. In Java, classes are organized in packages and if you want to use them in your program, you need to import them.

For exampleL

Suppose you have a package called com.example.admin and you have a class called Sample

public class Sample {
   
    private String sampleString;

    public Sample(String sampleString){
        this.sampleString=sampleString; 
    }

    public getString(){
        return sampleString;    
    }

} 

Now, if you want to use the Sample class in another class outside this package, then you will use the import statement:

import com.example.admin.Sample

This is your way of telling the Java compiler, I want to use the Sample class which is inside the package com.example.admin.

There are also situations where you won't be able to import classes, because they are private in the package, so no one outside the package will be able to access them.

These are good articles you can look at:

Now, onto the Autowired annotation. This is a Spring annotation. It has meaning in the Spring world. Using this annotation, you tell Spring, using your dependency injection facilities, please inject the object of this specific type managed by the Spring's container.

For example, if we add the annotation @Component to the above class Sample we talked about.

@Component
public class Sample {

    private String sampleString;

    public Sample(String sampleString){
        this.sampleString=sampleString; 
    }

    public getString(){
        return sampleString;    
    }

}

Now, if the above class is part of the classpath scanning done by Spring, it will be managed by it. Then, you can Autowire it in any other Spring managed component. I would recommend you to read the following links to understand Spring a little bit better.

Solution 2:[2]

import, as the name implies, just imports the dependency. @Autowired creates an instance of that dependency and injects it into our component.

Say we have a certain class B like:

class B {
    public B() {}

    public void sayHi() {
        System.out.println("Hello World");
    }
}

Import

import ClassB;

class A {
    private B bRef;

    public A() {}

    public void sayHi() {
        bRef.sayHi();
    }

    public static void main(String[] args) {
        A a = new A();
        a.sayHi(); // NullPointerException -> we bRef is null
    }
}

@Autowired

import ClassB;

@Component
class A {
    // autowiring member variables is considered bad but just to
    // illustrate the point
    @Autowired
    private B bRef;

    public A() {}

    public void sayHi() {
        bRef.sayHi();
    }

    public static void main(String[] args) {
        A a = new A();
        a.sayHi(); // Outputs: Hello World
        // bRef was constructed and injected into our component
    }
}

Solution 3:[3]

Import helps us to create an instance of other class and facilitates to use its methods. But where as @Autowired helps us to autowire/join the bean/dependency created in container of that other class(which is a component of container denoted by @component annotation).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Rotteveel
Solution 2 StaticBeagle
Solution 3 Arshad