0

I am working on a Spring MVC project where I am dealing with different types of services,Repositories i.e classes annotated with @Service and @Repository. I am confused with a couple of questions:

  • When to use @AutoWired annotation?

    I have seen various repositories using this:

      CourseRepository crepo=new CourseRepository();
    

    and I have seen this also

    @AutoWired
    private CourseRepository crepo;
    

    Which one of the above options should be used to get an instance of repository in Service class?

  • Can I use @AutoWired for classes which are not annotated with @Repository or @Service?

I am a beginner in this java world.Any help will be highly appreciated. Thanks

2
  • Yes you can use @Autowired. Commented Jun 21, 2021 at 15:49
  • With Spring you don't really have to create an object explicitly using 'new' unless you are initializing a Bean. You can always annotate fields with Autowired and Spring will automatically search an implementation and create an object for you, provided you don't have multiple implementations. In that case, you will have to use @Qualifier.
    – bluelurker
    Commented Jun 21, 2021 at 16:09

3 Answers 3

2

You use new for data objects, which in most modern architectures are passive (they're not "active records"). Everything else is a service object, and you should inject those. (The one place that you do use new is with an @Bean method, which is a "factory" that creates the service object; in this case you normally pass the dependencies as method parameters.)

Note that it is recommended to use constructor injection instead of field injection; it makes your code easier to test, and it eliminates the possibility of certain kinds of errors. In fact, if using constructor injection, it's not required to have any Spring annotations in your service classes at all; beans can be registered using @Import instructions or @Bean methods on a configuration class.

2
  1. You should @Autowire the dependencies instead of instantiating it yourself. Doing so, service and repo layer will be loosely coupled. Moreover, a mock repository can be easily injected in service's JUnit test class if dependency is autowired. To conclude, use below:

    @Autowired private CourseRepository crepo;

  2. A class not annotated with any of below stereotype annotations will not be in Spring's IoC (Inversion of Control) container. Hence, no point in autowiring in a class that is not annotated with any of below annotations.

    @Component, @Controller, @Service, @Repository

1

Dependency injection means that the framework is the one who handles the classes instantiation and the object of that class is going to be injected (thanks to @Autowired annotation) in the class where you need it. In other words, you do not need to instantiate service and repository classes by yourself using new operator, you just need to tell the framework that those classes need to be injected and that's why you use @Autowired annotation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.