Spring可以理解@Inject替代Weld作为JSR

编程入门 行业动态 更新时间:2024-10-26 21:23:29
本文介绍了Spring可以理解@Inject替代Weld作为JSR-299实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从几个网页中注意到,显然Spring 3.0支持JSR-330的@Inject。由于我们确实想在Web应用程序和独立应用程序的库中使用JSR-299语法进行依赖项注入,并且有Weld的替代品,因此Spring可以做到这一点。

作为Spring的新手,我尝试下载Spring Framework发行版,并将所有jar放在Eclipse构建路径上。没有Inject注释,因此我现有的使用Weld的测试项目无法编译。

可以用Spring完成吗?我需要做些什么才能使其运行?

(我知道Guice最终也将支持此功能。目前仅在SVN中,如果有

这是可以做到的。必须单独下载JSR-330 jar,然后使用cglib解析手动编写的@Configuration类,再加上公共记录实现。

与Weld的最大区别似乎是布线需要手动编写,而不是神奇地发现(比较麻烦,但可能会使应用程序更健壮),而且启动时间要短得多。我对Spring还是很陌生-有没有办法自动发现@Configuration类?

解决方案

从 Spring 3.0.x参考文档:

在下面的示例中,可以使用JSR 330的@Inject注释代替Spring的@Autowired。与Spring的@Autowire注解不同,@Inject没有必需属性,Spring的@Autowire注释具有必需属性,用于指示要注入的值是否可选。如果您在类路径上具有JSR 330 JAR,则会自动启用此行为。

因此,您可以使代码与DI框架无关通过使用@Inject,但是您仍然需要在项目中的 javax.inject 类中包含一个jar,因为Spring本身并不提供它们。您可以在 JSR-330的Google Code网站的下载部分中找到相关的jar。 / p>

I have noticed from several web pages that apparently Spring 3.0 supports @Inject from JSR-330. As we would really like to use JSR-299 syntax for dependency injection in our libraries for both web apps and stand-alone applications, and have alternatives to Weld, it would be nice if Spring could do this.

Being a novice to Spring, I tried downloading the Spring Framework distribution and put all jars on the Eclipse build path. No Inject annotation so my existing test project using Weld did not compile.

Can this be done with Spring? What do I need to do to get it running?

(I am aware that Guice eventually will support this too. It is only in SVN for now, and if there is an official Spring release which can, that would be better.)

It can be done. The JSR-330 jar must be downloaded seperately, and cglib to parse the manually written @Configuration classes, plus a commons logging implementation.

The largest difference from Weld seems to be that the wiring needs to be manually written instead of magically found (a bit more cumbersome, but may make more robust applications), plus the startup time is much less. I am still new to Spring - is there a way to have @Configuration classes autodiscovered?

解决方案

From the Spring 3.0.x Reference documentation:

JSR 330's @Inject annotation can be used in place of Spring's @Autowired in the examples below. @Inject does not have a required property unlike Spring's @Autowire annotation which has a required property to indicate if the value being injected is optional. This behavior is enabled automatically if you have the JSR 330 JAR on the classpath.

So you can make your code agnostic of the DI framework by using @Inject, but you still need to include a jar with the javax.inject classes in your project because Spring does not ship them itself. You can find the relevant jar in the downloads section at JSR-330's Google Code site.

更多推荐

Spring可以理解@Inject替代Weld作为JSR

本文发布于:2023-10-07 17:08:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Inject   Spring   JSR   Weld

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!