Java @Repository 和 @Autowired 用于什么。(春天)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3988114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what are @Repository and @Autowired used for. (Spring)
提问by mehmet6parmak
I am learning java for 3 months and sometimes i can not understand the usage purpose of something.
我学习了 3 个月的 Java,有时我无法理解某些东西的使用目的。
one topic was dependency injection and spring beans i figured out the finally =)
一个主题是依赖注入和 spring bean 我终于找到了 =)
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?
现在我对@Autowired 和@Repository 的两个注释感到困惑。首先什么是自动装配?那么我为什么要使用它们,使用它们和不使用它们之间有什么区别?
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
今天我尝试在 spring mvc 项目中使用 hibernate,由于项目中使用的其他 jar 文件的依赖关系,我不得不搜索大约 15 个(类未找到错误的原因)jar 文件。必须这样吗?这使得初学者很难学习 Java
thanks...
谢谢...
采纳答案by Bogdan
@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
@Repository 是一个注解,将特定的类标记为数据访问对象,从而阐明它的作用。同一类别的其他标记是@Service 和@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
@Autowired 是一个具有完全不同含义的注解:它基本上告诉 DI 容器注入依赖项。更多信息在http://apollo89.com/java/spring-framework-2.5.3/api/org/springframework/beans/factory/annotation/Autowired.html
编辑更多信息在教程点
或docs.spring.io
回答by Karan guptA
@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 和 @Repository 是两个不同的概念。1.@Repository:这定义了一个类作为一个存储库,一般来说你可以简单地使用@Component但具体定义,还有3个注解,如Controller,service和repository。主要有2个优点:1.如果你已经定义了(context:component-scan) 在 servlet.xml 中扫描定义的包并通过 spring 找到自己的包。2. Spring带来的更多好处,比如数据库访问错误翻译,所以主要定义为与你连接数据库的类一起使用,无论是hibernate还是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
@Autowired:通过spring在运行时注入依赖,意味着在一个类中,自动装配一个对象,并使用它,所以这个bean将自动生成而无需在xml文件中定义
回答by Arun Raaj
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").
@Autowired
: 这和<bean="xyz" autowire="byType">
你在配置文件中定义的一样。使用@Autowired 注释的引用变量(依赖项)将作为在@Configuration 类中找到的任何匹配@Bean 被Spring 容器注入。
加上用@Component、@Service、@Repository 注释的类也被视为 bean,因此它们的对象被注入到匹配的依赖项中。Spring 容器扫描您为“component-scan”或@ComponentScan("xyz") 提到的类中的bean。
@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.
@Repository
: 这也是spring-framework的注解。当您注释一个类@Repository 时,spring 容器会理解它是一个 DAO 类并将所有未经检查的异常(从 DAO 方法抛出)转换为 Spring DataAccessException。DAO 类是您编写方法以对 db 执行操作的类。