diff --git a/spring-framework-reference/src/beans-standard-annotations.xml b/spring-framework-reference/src/beans-standard-annotations.xml index b84a67c57aa..710d04aad59 100644 --- a/spring-framework-reference/src/beans-standard-annotations.xml +++ b/spring-framework-reference/src/beans-standard-annotations.xml @@ -2,30 +2,36 @@
- Using JSR 330 standard annotations + Using JSR 330 Standard Annotations - Starting from Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection). + Starting with 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 can add the following dependency to your file pom.xml: - - <dependency> - <groupId>javax.inject</groupId> - <artifactId>javax.inject</artifactId> - <version>1</version> - </dependency> - + + If you are using Maven, the javax.inject artifact is available + in the standard Maven repository + (http://repo1.maven.org/maven2/javax/inject/javax.inject/1/). + You can 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 { + + Instead of @Autowired, + @javax.inject.Inject may be used as follows: + + import javax.inject.Inject; + +public class SimpleMovieLister { private MovieFinder movieFinder; @@ -35,15 +41,18 @@ } // ... } + - As for @Autowired, it is possible to use @Inject at the class-level, field-level, method-level and constructor-argument level. + As with @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 { + 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; @@ -60,13 +69,12 @@
<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; + Instead of @Component, @javax.inject.Named may be used as follows: + import javax.inject.Inject; +import javax.inject.Named; - @Named("movieListener") - public class SimpleMovieLister { +@Named("movieListener") +public class SimpleMovieLister { private MovieFinder movieFinder; @@ -77,14 +85,17 @@ // ... } + -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; +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 { +@Named +public class SimpleMovieLister { private MovieFinder movieFinder; @@ -93,39 +104,38 @@ It is very common to use @Component without speci 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> - +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: + + 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 annotations vs. standard annotations - - Spring - javax.inject.* - javax.inject restrictions / comments @@ -133,58 +143,51 @@ When using @Named, it is possible to use componen @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. + The 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 a + scope other 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. + javax.inject also provides a + @Scope annotation. + Nevertheless, this one is only intended to be used for creating your own annotations. @Qualifier - @Named - - + @Value - - - - + no equivalent @Required - - - - + no equivalent @Lazy - - - - + no equivalent @@ -193,4 +196,5 @@ When using @Named, it is possible to use componen + \ No newline at end of file