1

I was wondering what is the difference between creating a new class and injecting it with the @Autowired annotation and creating a class and take the object of this class and using its methods. Is there any techical reason(i.e. faster access etc)?

Service case:

 @Service
 public class AuthorService implements AuthorServiceInterface {


   //some methods
 }

Simple Class case:

 public class AuthorService implements AuthorServiceInterface {


   //some methods
 }

If i want to call the first one in another class i have to write:

public Class myclass{

    @Autowired
    AuthorService authorservice;


} 

In the second case i have to write:

public Class myclass{


    AuthorService authorservice = new AuthorService():

} 

Whats the difference between these two cases?

Nick Robertson
  • 1,047
  • 4
  • 18
  • 41
  • Could you explain what you mean by two code snippets? – JB Nizet Jun 22 '12 at 15:40
  • @JB Nizet I update my answer. – Nick Robertson Jun 22 '12 at 16:01
  • @Nick Robertson because you want to take advantage of spring `dependency injection` to create objects for you. It certainly looks more nicely with `@Autowired`. More loosely coupled and high cohesive code. – ant Jun 22 '12 at 16:06
  • 1
    please see this answer, http://stackoverflow.com/questions/131975/what-are-the-benefits-of-dependency-injection-containers and red 20 mins ago JB Nizets answer pretty much sums it.sorry I have to leave don't have time to write xamples – ant Jun 22 '12 at 16:31

1 Answers1

3

The first snippet uses dependency injection, and the second doesn't. Dependency injection allows

  • decoupling MyClass from the concrete implementation of AuthorService which would allow switching implementations depending on the environment for example
  • using a singleton (or session-scoped or request-scope) AuthorService rather than reinstantiating one each time
  • injecting a mock AuthorService implementation when unit-testing MayClass
  • injecting a proxy around the concrete AuthorService instance, which could
    • verify authorizations
    • start a transaction before each method call and commit/rollback it after the method call
    • log the method calls
    • measure the time taken by methods and compute statistics
    • invoke an AuthorService on another machine, using RMI or HttpInvoker
    • ...

Note that you should autowire AuthorServiceInterface, and not AuthorService, in MyClass.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I understand your answer.That i want to ask now,if i use the first approach the process will become faster? – Nick Robertson Jun 22 '12 at 16:13
  • @Nick Robertson Short answer probably. But that is micro optimization and plus if the project becomes large enough it may be hectic to maintain it. – ant Jun 22 '12 at 16:15
  • @ant what do you mean when you say "it may be hectic to maintain it"? – Nick Robertson Jun 22 '12 at 16:29
  • 1
    It could become faster. But performance is not the reson why you use dependency injection. Anyway, this is not where time is specnt in a typical application. Concentrate on IO, databse queries, interprocess calls, complex algorithms. – JB Nizet Jun 22 '12 at 17:20