简体   繁体   English

Spring,@ Autowired,@ Resource和<property>

[英]Spring, @Autowired, @Resource and <property>

This week I've been trying to learn Spring, JBoss, Maven, JPA and Hibernate and I've had a lot of fun with it. 这个星期我一直在努力学习Spring,JBoss,Maven,JPA和Hibernate,我玩得很开心。 I am somewhat confused over the many different ways to inject resources in a class though. 我对在类中注入资源的许多不同方法感到有些困惑。 Until this week I was not even aware that you could inject resources in any other way than using the <property> tag in your Spring XML configuration. 直到本周,我甚至不知道你可以以任何其他方式注入资源,而不是在Spring XML配置中使用<property>标签。

<bean id="catalogService" class="com.idbs.omics.catalog.service.CatalogService">
    <property name="termDao" ref="termDao"></property>
</bean>

When I started experimenting with JPA I encountered @PersistenceContext , but that seems to be a special case so fair enough. 当我开始尝试使用JPA时,我遇到了@PersistenceContext ,但这似乎是一个非常公平的特殊情况。 Then I started reading up on Spring's testing framework and I saw the first example that used @Resource(name="catalogService") and then in a Web Service example @Autowired crashed the party! 然后我开始阅读Spring的测试框架,我看到了第一个使用@Resource(name="catalogService")例子,然后在Web服务示例中@Autowired崩溃了派对!

**The Question!**

So what is the difference between all these and is there right and wrong situation to use them in? 那么所有这些之间的区别是什么,是否有正确和错误的情况使用它们? I guess I'm looking for a best practice here. 我想我在这里寻找最好的做法。

Cheers all 干杯全都

@Autowired , @Resource and @Inject @Autowired@Resource@Inject

I think the part of the Spring Reference that you need to read is 我认为你需要阅读的Spring Reference部分是

3.9 Annotation-based container configuration 3.9基于注释的容器配置


There are many sets of similar annotations. 有许多类似的注释。 Often, there is a Spring and a non-Spring version that do the same thing. 通常,有一个Spring和一个非Spring版本可以做同样的事情。 Spring tries to embrace standards whenever they are available, but often the Spring guys came up with their own ideas before a standard appeared. Spring会尽可能地接受标准,但是在标准出现之前,Spring会经常提出自己的想法。 Example: Spring supports its own @Autowired annotation, but also the new @Inject annotation from JSR-330, as well as the JSR-250 @Resource annotation (all of which can be used to do the same thing). 示例:Spring支持自己的@Autowired注释,但也支持JSR-330的新@Inject注释,以及JSR-250 @Resource注释(所有这些注释都可用于执行相同的操作)。

The key concept here is that Spring doesn't force you to use its own code, but supports many different ways without coupling your application to Spring. 这里的关键概念是Spring不会强迫您使用自己的代码,但支持许多不同的方式而不将您的应用程序与Spring耦合。 (There are still a few annotations that have no non-Spring equivalent, like @Transactional , but if you want to, you can add that functionality via XML instead, so you can keep your application 100% Spring free and still use many convenience annotations and of course Spring wiring and lifecycle management behind the scenes. (还有一些注释没有非Spring等价,比如@Transactional ,但如果你愿意,你可以通过XML添加这个功能,这样你就可以保持你的应用程序100%Spring免费并仍然使用许多便利注释当然还有幕后的Spring布线和生命周期管理。

The other aspect of this is when to use annotations and when to use XML spring wiring files. 另一方面是何时使用注释以及何时使用XML spring连接文件。

My view is that this is a trade-off between code readability and the ability to reconfigure. 我的观点是,这是代码可读性和重新配置能力之间的权衡。

  • If you use annotations, then you can see from just the source code what is wired to what. 如果您使用注释,那么您可以从源代码中看到连接到什么的连接。 By contrast, if you use XML wiring files, you have two places to look. 相比之下,如果您使用XML布线文件,则需要查看两个位置。

  • If you use XML wiring files, you can make configuration changes by modifying (or with Maven overlaying) the XML wiring files. 如果使用XML布线文件,则可以通过修改(或使用Maven覆盖)XML布线文件来进行配置更改。 If you are bold, you can even do this on a deployed webapp. 如果你是大胆的,你甚至可以在部署的webapp上执行此操作。 By contrast, changing the IoC wirings when annotations are used requires you to modify the Java files and recompile. 相反,在使用注释时更改IoC接线需要您修改Java文件并重新编译。

(Aside: in the case of the second bullet, the ideal situation would be to have a nice GUI-based tool for reconfiguring the wirings that could be run in either your development sandbox, or the deployed webapp folder. Does anyone know of such a tool?) (旁白:在第二个子弹的情况下,理想的情况是有一个很好的基于GUI的工具,用于重新配置可以在开发沙箱或部署的webapp文件夹中运行的接线。有没有人知道这样的工具?)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM