0

what is the main difference between injecting objects with @Autowired and injecting without it ? I know that spring will initialize the bean , but what it is really offering ?

Anas Hadhri
  • 159
  • 1
  • 2
  • 9
  • when you say without autowired, do you mean using `@configuration` + `@bean`, and injecting into constructor or manually into setter? – Coderino Javarino Dec 17 '19 at 20:14
  • public class A { B objB; A(){initobject B} calculAmout(B objB) ; } , imagine that you have a class A where B is a bean and you initialize it mannualy – Anas Hadhri Dec 17 '19 at 20:54

2 Answers2

1

There are several ways to configure Spring beans and inject dependencies using Spring. One way is by using constructor injection, where the constructor of your Spring bean has arguments which are the dependencies that should be injected:

@Component
public class MyBean {
    private final SomeDependency something;

    @Autowired
    public MyBean(SomeDependency something) {
        this.something = something;
    }
}

However, since Spring 4.3, it is not necessary anymore to use @Autowired on such a constructor (click link for Spring documentation). So you can write it without the @Autowired:

@Component
public class MyBean {
    private final SomeDependency something;

    public MyBean(SomeDependency something) {
        this.something = something;
    }
}

This will work exactly the same as the code above - Spring will automatically understand that you want the dependency to be injected via the constructor. The fact that you can leave out @Autowired is just for convenience.

So, to answer your question: there is no difference.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • okey BUT dependecy injection is independent from SPRING . when we injectect a service in a constructor in simple java class , it is dependency injection But I don't see why we speak about dependecy injection when we use Autowired in spring . for me autowired is not used for dependecy injection , it has other consequences – Anas Hadhri Dec 17 '19 at 21:30
  • @AnasHadhri I don't know why you think that `@Autowired` is not used for dependency injection - dependency injection is exactly the purpose of the `@Autowired` annotation. By using the `@Autowired` annotation, you tell Spring where you want dependencies injected. – Jesper Dec 17 '19 at 21:33
  • https://www.tutorialsteacher.com/ioc/dependency-injection here it speaks about the pattern in general , autowired done other things I think – Anas Hadhri Dec 17 '19 at 21:33
  • because you can inject beans without autowired ( juste you have to initialize beans mannualy ) – Anas Hadhri Dec 17 '19 at 21:34
  • That link points to a tutorial about C#. If you want to learn Spring (in Java) then it's better to use the [Spring documentation](https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/). – Jesper Dec 17 '19 at 21:35
  • Still: `@Autowired` is one of the main ways you do dependency injection in Spring. The fact that there are also other ways to do dependency injection, without the `@Autowired` annotation, does not mean that using `@Autowired` is something else than dependency injection. – Jesper Dec 17 '19 at 21:39
  • just please try to understand my point , what is the difference if i don't use @autowired and I put my service B in the constructor the class A then I initialize object B in A contructor with B=new BImpl() ... – Anas Hadhri Dec 17 '19 at 21:39
  • If you are manually creating an object with `new BImpl()` then you are not using dependency injection. Dependency injection means that you let Spring create your dependencies and provide them to your bean, instead of creating the dependencies yourself using `new`. – Jesper Dec 17 '19 at 21:40
  • I'm sorry but in an interview , when we ask about what does dependency injection mean , we don't ask about any language , no java , no c# . Just the concept – Anas Hadhri Dec 17 '19 at 21:43
  • OK, but you were specifically asking about the Spring `@Autowired` annotation. See [What is dependency injection?](https://stackoverflow.com/questions/130794/what-is-dependency-injection?rq=1) – Jesper Dec 17 '19 at 21:44
  • okey what difference if I create my dependecies myself or If I use spring in order to use dependecy injection? – Anas Hadhri Dec 17 '19 at 21:53
  • maybe the only difference is that I cannot mock it on test If I define my own dependencies – Anas Hadhri Dec 17 '19 at 21:57
  • So, your question is really: what is dependency injection and why should I use it? You'll find a lot of information on the web about that, for example here: [A quick intro to Dependency Injection: what it is, and when to use it](https://www.freecodecamp.org/news/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f/). Making dependencies easy to mock is indeed one of the benefits. – Jesper Dec 17 '19 at 21:59
0

@Autowired (so the injection) in some situation cannot be used, an example is if your autowired bean not ready because of some async stuff but in the target bean you want to use that.

So in this situation do not use inject (@Autowired) it is better to inject the ApplicationContext and in the exact moment get your bean from there by name or by class (there is a lot off possibilities there).

You can consider the @Autowired with the @Lazy annotation too.

Antal Attila
  • 588
  • 6
  • 18