简体   繁体   English

关于在Spring Framework中使用** @Autowired **注释和接口声明的一些疑问

[英]Some doubts about the use of **@Autowired** annotation and interface declaration in Spring Framework

I am quite new in Spring framework and I have some questions about the use of @Autowired annotation and interface declaration. 我是Spring框架中的新手,我对使用@Autowired注释和接口声明有一些疑问。

Referring to this example: 参考这个例子:

http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/ http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/

I know that @Autowired annotation can be used to automatically link a bean on a property. 我知道@Autowired注释可用于自动链接属性上的bean。

In the previous example I have the following situation: 在前面的例子中,我有以下情况:

I have a ContactDAO interface and it's implementation class named ContactDAOImpl 我有一个ContactDAO接口,它的实现类名为ContactDAOImpl

Next in the class ContactServiceImpl there is this variable annoted using @Autowired : 接下来在ContactServiceImpl类中,使用@Autowired对此变量进行了注释:

@Autowired
private ContactDAO contactDAO;

My first doubt is related to the fact that ContactDAO is an interface so what am I wiring? 我的第一个疑问是因为ContactDAO是一个接口,所以我接线的是什么? The concrete type: ContactDAOImpl ? 具体类型: ContactDAOImpl If yes, is the Spring Framework that do it? 如果是的话,Spring Framework是否可以做到这一点?

The second doubt is related to the fact that in the spring-servlet.xml configuration file there is not a bean definizion for the ContactDAO orf ContactAOImpl class...why? 第二个疑问与spring-servlet.xml配置文件中没有针对ContactDAO orf ContactAOImpl类的bean定义有关...为什么? Is it because ContactDAOImpl class is annoted using @Repository annotation? 是不是因为ContactDAOImpl类使用@Repository注解annoted?

Thanks 谢谢

Andrea 安德里亚

My first doubt is related to the fact that ContactDAO is an interface so what am I wiring? 我的第一个疑问是因为ContactDAO是一个接口,所以我接线的是什么? The concrete type: ContactDAOImpl ? 具体类型:ContactDAOImpl? If yes, is the Spring Framework that do it? 如果是的话,Spring Framework是否可以做到这一点?

Spring will autowire an implementation of the interface for you, as long as there's only one matching implementation. 只要只有一个匹配的实现,Spring将自动为您提供接口的实现。 There's also a way to match a single implementation from multiple candidates to your autowiring by using @Qualifier with @Autowired and naming the implementation. 通过将@Qualifier@Autowired一起使用并命名实现,还有一种方法可以将来自多个候选项的单个实现与自动装配相匹配。

The second doubt is related to the fact that in the spring-servlet.xml configuration file there is not a bean definizion for the ContactDAO orf ContactAOImpl class...why? 第二个疑问与spring-servlet.xml配置文件中没有针对ContactDAO orf ContactAOImpl类的bean定义有关...为什么? Is it because ContactDAOImpl class is annoted using @Repository annotation? 是因为使用@Repository注释来注释ContactDAOImpl类吗?

If you're using annotations ( @Component , @Repository , @Service , @Controller ) in your implementations for configuration, you don't need to explicitly define the bean in the xml (although you can do that also). 如果您在配置实现中使用注释( @Component @Repository@Service @Component@Repository@Service @Controller ),则不需要在xml中显式定义bean(尽管您也可以这样做)。

Edit: this old answer of mine might shed some more light about using annotations in Spring. 编辑:我的这个旧答案可能会更多地阐明在Spring中使用注释。

The answers to your two questions are Yes, and Yes. 你的两个问题的答案是肯定的,是的。

In fact, you might not have an instance of ContactDAOImpl autowired in the service, but an instance of a proxy, which deletages to an instance of ContactDAOImpl. 实际上,您可能没有在服务中自动装配ContactDAOImpl的实例,而是一个代理实例,它会卸载到ContactDAOImpl的实例。 The proxy will typically handle transactions, translate exceptions, etc. 代理通常会处理事务,转换异常等。

And the @Repository annotation is an alternative (simpler) way to declare a Spring bean. 并且@Repository注释是声明Spring bean的另一种(更简单的)方法。 It works only if you have an element in the Spring xml file telling it to discover annotated beans. 它只有在Spring xml文件中有一个元素告诉它发现带注释的bean时才有效。

Spring will autoscan all your classes and find all annotated classes and register them, this in your spring config will tell it to do that: Spring将自动扫描所有类并查找所有带注释的类并注册它们,这在spring配置中会告诉它这样做:

<context:component-scan base-package="my.base.package" />

Therefore you do not need to declare your @Repository in your configuration file. 因此,您无需在配置文件中声明@Repository

Onto the first part of your question, this is the unpinning of the IOC pattern ; 在问题的第一部分,这是IOC模式的重新定位 ; your Service class is only aware of the interface of the DAO, this means that it is not dependent on the implementation. 您的Service类只知道DAO的接口,这意味着它不依赖于实现。

During scanning Spring will find all of your annotated classes and when you ask for an @Autowired then it will attempt to find a class that your have annotated that is an implentor of the interface you have asked to have Autowired. 在扫描期间,Spring会找到所有带注释的类,当你要求@Autowired它会尝试找到你已经注释的类,这个类是你要求自动装配的接口的实现者。

Have a look at the Spring documentation on Annotation Configuration . 查看有关Annotation配置的Spring文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于RowMapper在Spring Framework应用程序中使用JDBC的一些疑问 - Some doubts about RowMapper use in JDBC in a Spring Framework application 对Spring框架中JDBC逻辑的一些怀疑 - Some doubts about the logic of JDBC in Spring Framework 关于这个 Spring Batch @Scheduled() 注解以及如何手动启动 Spring Batch 作业的一些疑问? - Some doubts about this Spring Batch @Scheduled() annotation and how to manually start a Spring Batch job? 使用 Spring Autowired 注解的要求 - Requirements to use Spring Autowired annotation 关于Spring MVC中的请求映射的一些疑问 - Some doubts about request mapping in Spring MVC 对春豆萌芽和破坏的一些怀疑 - Some doubts about Spring bean inizialitazion and destruction 是否可以在@Service中使用Spring 3 @Autowired注释? - Is it possible to use Spring 3 @Autowired annotation inside @Service? 关于Java SWING和Swing应用程序框架的一些疑问 - Some doubts about Java SWING and Swing Application Framework 注释类型声明中的@Autowired用法 - @Autowired usage in Annotation type declaration 关于Spring Application Context的Java配置(依赖注入)的一些疑问 - Some doubts about Java Configuration of the Spring Application Context (dependency injection)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM