diff --git a/spring-framework-reference/src/beans-standard-annotations.xml b/spring-framework-reference/src/beans-standard-annotations.xml new file mode 100644 index 0000000000..19b17735ed --- /dev/null +++ b/spring-framework-reference/src/beans-standard-annotations.xml @@ -0,0 +1,196 @@ + + +
+ Using JSR 330 standard annotations + + Starting from Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection). + Those annotations are scanned in the same way as the Spring annotations. You just need to have the relevant jars in your classpath. + + + If you are using Maven, the javax.inject artifact is available on the standard Maven repository (http://repo1.maven.org/maven2/javax/inject/javax.inject/1/). You just need to add the following dependency to your file pom.xml: + + <dependency> + <groupId>javax.inject</groupId> + <artifactId>javax.inject</artifactId> + <version>1</version> + </dependency> + + + +
+ Dependency Injection with <interfacename>@Inject</interfacename> and <interfacename>@Named</interfacename> + + Instead of @Autowired, javax.inject.Inject may be used as follows: + + + import javax.inject.Inject; + public class SimpleMovieLister { + + private MovieFinder movieFinder; + + @Inject + public void setMovieFinder(MovieFinder movieFinder) { + this.movieFinder = movieFinder; + } + // ... +} + + As for @Autowired, it is possible to use @Inject at the class-level, field-level, method-level and constructor-argument level. + + If you would like to use a qualified name for the dependency that should be injected, you should use the @Named annotation as follows: + + import javax.inject.Inject; + import javax.inject.Named; + + public class SimpleMovieLister { + + private MovieFinder movieFinder; + + @Inject + public void setMovieFinder(@Named("main") MovieFinder movieFinder) { + this.movieFinder = movieFinder; + } + // ... +} + + +
+ +
+ <interfacename>@Named</interfacename>: a standard equivalent to the <interfacename>@Component</interfacename> annotation + + Instead of @Component, javax.inject.Named may be used as follows: + + import javax.inject.Inject; + import javax.inject.Named; + + @Named("movieListener") + public class SimpleMovieLister { + + private MovieFinder movieFinder; + + @Inject + public void setMovieFinder(MovieFinder movieFinder) { + this.movieFinder = movieFinder; + } + // ... +} + + +It is very common to use @Component without specifying a name for the component. @Named can be used in a similar fashion: + + import javax.inject.Inject; + import javax.inject.Named; + + @Named + public class SimpleMovieLister { + + private MovieFinder movieFinder; + + @Inject + public void setMovieFinder(MovieFinder movieFinder) { + this.movieFinder = movieFinder; + } + // ... +} + + + +When using @Named, it is possible to use component-scanning in the exact same way as when using Spring annotations: + + <beans> + <context:component-scan base-package="org.example"/> + </beans> + + +
+
+ Limitations of the standard approach + When working with standard annotations, it is important to know that some significant features are not available as shown in the table below: + + + Spring annotations vs standard annotations + + + + + + + + + + + + Spring + + javax.inject.* + + javax.inject restrictions / comments + + + + + + @Autowired + + @Inject + + @Inject has no 'required' attribute + + + @Component + + @Named + + + + + @Scope("singleton") + + @Singleton + + + + jsr-330 default scope is like Spring's prototype. However, in order to keep it consistent with Spring's general defaults, a jsr-330 bean declared in the Spring container is a singleton by default. In order to use another scope than singleton, you should use Spring's @Scope annotation. + + + javax.inject also provides a @Scope annotation. Nevertheless, this one only aims to be used for creating your own annotations. + + + + + @Qualifier + + @Named + + + + + @Value + + - + + no equivalent + + + @Required + + - + + no equivalent + + + @Lazy + + - + + no equivalent + + + +
+
+ +
+
\ No newline at end of file diff --git a/spring-framework-reference/src/beans.xml b/spring-framework-reference/src/beans.xml index d4be73904d..308520e6fa 100644 --- a/spring-framework-reference/src/beans.xml +++ b/spring-framework-reference/src/beans.xml @@ -942,6 +942,9 @@ List userList service.getUsernameList(); + +