Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowired vs import

I am very new to spring boot and I am trying to grasp the concept of it. Now I came across @Autowired in it. I understood pretty much about it.

Like, when you write @Autowired, things happen in two passes and in the second pass spring injects beans.

Now, I have this example;

Class Abc {

    @Autowired
    private Xyz xyz;

    PSVM(String...z) {
        xyz.hello();
    } 
}

Import basically imports the code into the file.

So,

Import com.tilak.Xyz;

Class Abc {
    private Xyz xyz;

    PSVM(String...z) {
        xyz = new Xyz();
        xyz.hello();
    }
}

Why should/ shouldn't I go with the latter one?

Is there any advantage in doing the first one? Where should I use the first one and where should I use the second one?

like image 630
Tilak Raj Avatar asked Dec 14 '22 14:12

Tilak Raj


2 Answers

Its basically about object creation. When you Autowire something, that particular bean is managed by the spring context and only one instance of particular class will be constructed by spring context and that particular instance will be provided when you use Autowire for that class type Objects. But when you import something into your class, that does not mean that you have an instance of that particular class; you have to initialize it on your own. And if you want to treat it as a singleton object, basically you will be writing whole code for it.

And more over there are basic differences between them like if you want to access static variables in a class, you will not need an instance of that class. (you can access them by class reference) In that case you dont need to Autowire but you need Import statement.

But on the other hand if you are interested in experiencing power of spring, like Autowire Configurations and may be validate them and all those cool functionalities that spring provides, you need to make your class a component and Autowire as you want.

like image 187
Damith Avatar answered Dec 28 '22 01:12

Damith


I believe you are confusing Spring and the Java language.

Autowire has to do with "Code injections". (Just like you described.) And is part of the Spring framework.

However, the import (with a lowercase i) is part of the Java language. And like you stated, makes other code available to be used in the class where you added it. import will be used regardless if you are using the Spring framework.

Both of these statements have completely different purposes and are used with other.

For example. The class you are autowiring will also be imported (if it is in a different Java package).

But that being said, there is also a @Import annotation in the Spring framework which is used for importing Spring Configuration classes. I am guessing this is where the confusion originated.

I would strongly suggest running through a quick intro to Java course. This will make you learning Spring a lot more pleasant :)

like image 28
Reg Avatar answered Dec 28 '22 01:12

Reg