what are @Repository and @Autowired used for. (Spring)

55,178

Solution 1

@Repository is an annotation that marks the specific class as a Data Access Object, thus clarifying it's role. Other markers of the same category are @Service and @Controller

@Autowired is an annotation with a completely different meaning: it basically tells the DI container to inject a dependency. More info at http://apollo89.com/java/spring-framework-2.5.3/api/org/springframework/beans/factory/annotation/Autowired.html
Edit More info at tutorialpoint
or docs.spring.io

Solution 2

Both the annotations have different purposes to be used.

@Autowired: This is same as <bean="xyz" autowire="byType"> you define in the configuration file. The reference variable (dependency) that is annotated with @Autowired, will be injected by Spring container as any matching @Bean found in @Configuration class.
Plus the classes annotated with @Component, @Service, @Repository are too considered as beans so their objects are injected into the matching dependencies. Spring container scans the beans in the classes you mentioned for "component-scan" or @ComponentScan("xyz").

@Repository: This is also a spring-framework's annotation. When you annotate a class @Repository, spring container understands it's a DAO class and translates all unchecked exceptions (thrown from DAO methods) into Spring DataAccessException. DAO class is the class where you write methods to perform operations over db.

Solution 3

@Autowired and @Repository are very 2 different concepts. 1.@ Repository: This define a class to be a repository, In general term you can use simply @Component but to define specifically, there are 3 more annotations like Controller,service and repository.Mainly 2 advantages: 1.If you have defined(context:component-scan)in servlet.xml to scan the defined package and find its own by spring. 2. More advantages you get from spring like database access error translation, so it is mainly defined to use with class in which you are connecting with database either with hibernate or jdbc.

@Autowired: to inject dependency at run-time by spring, means in a class, autowire a object ,and use it ,so this bean will automatically be made without defining in xml file

Share:
55,178

Related videos on Youtube

mehmet6parmak
Author by

mehmet6parmak

Updated on July 09, 2022

Comments

  • mehmet6parmak
    mehmet6parmak almost 2 years

    I am learning java for 3 months and sometimes i can not understand the usage purpose of something.

    one topic was dependency injection and spring beans i figured out the finally =)

    now i confused with the two annotations @Autowired and @Repository. First What does Autowiring mean? then Why should i use them and what is the difference between using them and not using?

    Also today i tried to use hibernate in a spring mvc project and i had to search for about 15(cause of class not found errors) jar files beacuse of the dependencies of other jar files used in the project. is this had to be this way? this makes learning java very hard for the beginners

    thanks...

    • Sebastian Piu
      Sebastian Piu over 13 years
      Those annotations are spring annotations, Autowired means that the Ioc context will automatically inject dependencies if possible, and repository I believe is a stereotype so you can add Exception rewriting and other spring dataaccess related components to it. Add the spring tag to your post and you may get some more answers!
  • mehmet6parmak
    mehmet6parmak over 13 years
    so repository does not provide any functionality am i right? What does role mean? for @controller there is something happening when we use it what is happening when we use repository?
  • Bogdan
    Bogdan over 13 years
    it actually does, it makes the annotated class eligible for DataAccessException translation.
  • Eldelshell
    Eldelshell over 11 years
    @Bogdan FYI: That link is broken.
  • avijendr
    avijendr about 8 years
    Not 100% correct - @Repository Indicates that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects". Teams implementing traditional J2EE patterns such as "Data Access Object" may also apply this stereotype. See the point "may also apply"