-2

I'm totally lost on this topic . I've researched Spring Autowiring on internet trying to figure out what it does and everything I find is mostly saying "Spring Autowiring helps you autowire collaborating Beans " and thats it . I would greatly appreciate if someone can explain to me from scratch what is Spring Bean, What is Autowiring and how these two work together with examples and explanations of this examples. Don't just say that "Spring Autowiring is autowiring two beans " because I don't get it what it means .

What I understand right now is that lets say if have a simple class :

Public class Car(){
  public int numberOfWheels;
}

and we declare this class as a Bean, we than can create the instance of the object of this class without saying "new" keyword. So we can just declare it in a bean and insert value of this "numberOfWheels" property outside the java class . I might be super wrong here or using very bad example , because I've been trying to learn Spring Framework and it's just been very hard for me , so any help would be greatly appreciated.

shelo
  • 85
  • 1
  • 2
  • 9
  • Do you know what [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) is, more generally? – Andy Turner Mar 19 '18 at 13:47
  • Possible duplicate of [Understanding Spring @Autowired usage](https://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage) – Emre Acar Mar 19 '18 at 13:48
  • Emre Acar, I've read that topic before and still didn't understand – shelo Mar 19 '18 at 13:51

3 Answers3

0

Well, Spring is a Dependency Injection framework, so you should start learning about this.

DEPENDENCY INJECTION

This is a technique whereby one object supplies the dependencies of another object. Such as, for example: object a of type A need is dependent of an object b of type B. In a code example:

Customer it's dependent of Person:

public class Person {
    private String name;
    private String lastName;
}

public class Customer {
    private Person person;
}

In this case, any Customer object will have a Person object dependency.

Now, in Spring scope, all objects are named beans, so this is a bean, an Object that it's injected into Spring Context.

Spring provides a Dependency Injection mechanism that it's very easy. For our example, you can put put some of the following annotation: @Component, @Bean, @Service on class Person, and Spring will create an object of type Person with calling default constructor. Afer that, in class Customer, you can put @Autowired annotation on top of the Person person attribute. This annotation, will tell Spring to search for a specific bean of type Person that was injected into the Spring Context, and Spring using Reflection(searching by name) will find a Person object type that was creating by using one of the @Component, @Bean, @Service annotations. After that, Spring will pass the reference of the Person object that was found to the Customer object that requires it.

Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
0

Spring Bean is an object which is instantiated and maintained automatically by Spring Framework. They usually depend on each other in some way, and the dependencies therefore must be resolved.

Autowiring is one of the ways how to resolve these dependencies. You don't manually specify which concrete dependency should be provided to the class, you just choose the way how Spring should automatically find the correct dependency (by matching the class's property name and name of the desired bean, for instance).

Spring's documentation is very extensive and you can find a lot of useful information there. For more info about Spring Beans, you can read this, if you want to know more about autowiring, try this.

Jiri Kapoun
  • 313
  • 1
  • 9
-2

To basically, Spring has a bean pool which means Spring creates java objects and put them into a pool. Whenever you want to use an object, you fetch object from pool using @Autowired annotation.

Spring basically uses Reflection to create objects by its own, instead of passing objects from class to class. This is what basically Autowire is.

This concept is called Dependency Injection. Spring injects components, services, repositories whenever you want.

Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26