Can Spring understand @Inject replacing Weld as a JSR-299 implementation?
Asked Answered
B

2

6

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?

Brodeur answered 16/6, 2010 at 17:33 Comment(0)
H
4

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.

Heliostat answered 16/6, 2010 at 17:48 Comment(4)
I found the same snippet, but I cannot make it work. If you can, please share how you did it.Depersonalize
I won't be able to look at that code until tonight. Did you get the jar from the JSR's site? code.google.com/p/atinjectHeliostat
Apparently I cannot read. I did not download the JSR-330 jars separately. I'll try tomorrow.Depersonalize
I needed the JSR-330 jar, cglib and commons logging, plus some manual wiring.Depersonalize
P
2

The javax.inject package is not included as part of Spring 3, but it does support it if it's present.

If you look at the source for AutowiredAnnotationBeanPostProcessor, you'll see the constructor uses reflection to locate javax.inject.Inject, and logs a message if it finds it. There's no compile-time dependency on it.

You'll need to locate the JSR-330 JARs from some other source (e.g. the JavaEE 6 SDK).

Piker answered 16/6, 2010 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.