Spring可以理解@Inject替代Weld作为JSR-299实现吗? [英] Can Spring understand @Inject replacing Weld as a JSR-299 implementation?

查看:85
本文介绍了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-299实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆