Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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...

like image 579
mehmet6parmak Avatar asked Oct 21 '10 13:10

mehmet6parmak


People also ask

What is @repository used for in Spring?

Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.

Why @autowired is used in Spring?

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.

Where @autowired can be used?

Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. Autowiring can't be used to inject primitive and string values.

What is the @repository Annotation?

@Repository Annotation is a specialization of @Component annotation which is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects.


1 Answers

@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

like image 177
Bogdan Avatar answered Oct 17 '22 19:10

Bogdan