1

What is the difference, if I autowire a class and provide value and instantiate an object of class and provide some value? For example-

@Autowired
private UserService userService;
userService.findUser(userName, password);

And

User user = new user();
userService.findUser(user.getuserName(),user.getpassword());

What is the difference in Autowiring and sending the data and instantiating the object and sending the data to some service class?

I'm trying to clarify the concepts in spring.

Cœur
  • 34,719
  • 24
  • 185
  • 251
Protagonist
  • 1,559
  • 7
  • 33
  • 51
  • refer http://stackoverflow.com/questions/30908648/spring-autowired-vs-using-new-keyword-to-create-object – Kannan Thangadurai Nov 12 '15 at 07:51
  • 1
    Your example may be wrong, you have two different types. – RPresle Nov 12 '15 at 07:52
  • 2
    Can you please provide more complete examples? I'm not sure how your two examples relate, in first it's not clear where `userName` and `password` come from, in the second one it's unclear where `userService` comes from. – Jiri Tousek Nov 12 '15 at 07:52

4 Answers4

2

When you use @Autowired you are leaving it up to the Spring framework to find and instantiate the userService. This is usually controlled through some configuration file or some other configuration, which allows you to change the behaviour of your application without changing the code itself.

On the other hand, when you instantiate the object yourself, you are specifying which object you are after and what type of class you want. This could leave you with less ambiguous code since you know what type of object is being initialized, but to make a change in your application's behaviour you would need to change your code.

In essence, the first option is less coupled than the second option, which is usually the recommended way of building things.

npinti
  • 51,070
  • 5
  • 71
  • 94
2

Well, the main difference is that in case u use @Autowired the object is also created, however, it's created by container and container decide when to do that. I want to give you a simple example: You have four classes 1,2,3 and 4. Three of them (1,2,3) uses the 4th. So, if you use new(), it`s hard to decide where to create object(in class 1, or 2, or 3, or even in each of them) of 4th class. Moreover, later you can delete class with object initialization and other 2 classes won't work (in case you created one object). Autowired annotation injects the object but you don't initialize object in class, so no problems appear This is like the simplest answer.

quento
  • 1,026
  • 3
  • 20
  • 40
1

Your example doesn't make a lot of sense; this User class, which looks like some plain data object, isn't adding anything to the second snippet.

The idea of "autowiring" is that some class, like maybe a Web controller, will need a UserService in order to get its work done. When Spring autowires the UserService, it goes into the context and finds a matching object and provides it to the class that needs it. This is technically separate from creating the object.

That said, the best practice is to use constructor injection--simply declare the other objects you need as constructor parameters and annotate the constructor with @Autowired (or @Inject). Spring will know to look up all the dependencies you need and call the constructor with them. This means that it's also very simple to provide mocks of those objects for testing or development.

chrylis -cautiouslyoptimistic-
  • 72,004
  • 20
  • 117
  • 147
0

the above answers are good i would like to tell a major difference between them .the purpose of autowiring is to avoid the dependencies between the class if you are creating objects with new making a change to one class will effect all the classes.

Manoj Ramanan
  • 734
  • 5
  • 19