Using JSR 330 Standard Annotations

4.3.9.RELEASE Spring Framework 119 Component Offline public class CachingMovieCatalog implements MovieCatalog { ... } Note As with most annotation-based alternatives, keep in mind that the annotation metadata is bound to the class definition itself, while the use of XML allows for multiple beans of the same type to provide variations in their qualifier metadata, because that metadata is provided per-instance rather than per-class.

7.11 Using JSR 330 Standard Annotations

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. Note If you are using Maven, the javax.inject artifact is available in the standard Maven repository http:repo1.maven.orgmaven2javaxinjectjavax.inject1 . 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 Inject and Named Instead of Autowired , javax.inject.Inject may be used as follows: import javax.inject.Inject; public class SimpleMovieLister { private MovieFinder movieFinder; Inject public void setMovieFinderMovieFinder movieFinder { this .movieFinder = movieFinder; } public void listMovies { this .movieFinder.findMovies...; ... } } As with Autowired , it is possible to use Inject at the field level, method level and constructor- argument level. Furthermore, you may declare your injection point as a Provider , allowing for on- demand access to beans of shorter scopes or lazy access to other beans through a Provider.get call. As a variant of the example above: 4.3.9.RELEASE Spring Framework 120 import javax.inject.Inject; import javax.inject.Provider; public class SimpleMovieLister { private ProviderMovieFinder movieFinder; Inject public void setMovieFinderProviderMovieFinder movieFinder { this .movieFinder = movieFinder; } public void listMovies { this .movieFinder.get.findMovies...; ... } } 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 Namedmain MovieFinder movieFinder { this .movieFinder = movieFinder; } ... } Named and ManagedBean: standard equivalents to the Component annotation Instead of Component , javax.inject.Named or javax.annotation.ManagedBean may be used as follows: import javax.inject.Inject; import javax.inject.Named; NamedmovieListener ManagedBeanmovieListener could be used as well public class SimpleMovieLister { private MovieFinder movieFinder; Inject public void setMovieFinderMovieFinder 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: 4.3.9.RELEASE Spring Framework 121 import javax.inject.Inject; import javax.inject.Named; Named public class SimpleMovieLister { private MovieFinder movieFinder; Inject public void setMovieFinderMovieFinder movieFinder { this .movieFinder = movieFinder; } ... } When using Named or ManagedBean , it is possible to use component scanning in the exact same way as when using Spring annotations: Configuration ComponentScanbasePackages = org.example public class AppConfig { ... } Note In contrast to Component , the JSR-330 Named and the JSR-250 ManagedBean annotations are not composable. Please use Spring’s stereotype model for building custom component annotations. Limitations of JSR-330 standard annotations When working with standard annotations, it is important to know that some significant features are not available as shown in the table below: Table 7.6. Spring component model elements vs. JSR-330 variants Spring javax.inject. javax.inject restrictions comments Autowired Inject Inject has no required attribute; can be used with Java 8’s Optional instead. Component Named ManagedBean JSR-330 does not provide a composable model, just a way to identify named components. Scopesingleton Singleton 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 4.3.9.RELEASE Spring Framework 122 Spring javax.inject. javax.inject restrictions comments should use Spring’s Scope annotation. javax.inject also provides a Scope annotation. Nevertheless, this one is only intended to be used for creating your own annotations. Qualifier Qualifier Named javax.inject.Qualifier is just a meta-annotation for building custom qualifiers. Concrete String qualifiers like Spring’s Qualifier with a value can be associated through javax.inject.Named . Value - no equivalent Required - no equivalent Lazy - no equivalent ObjectFactory Provider javax.inject.Provider is a direct alternative to Spring’s ObjectFactory , just with a shorter get method name. It can also be used in combination with Spring’s Autowired or with non- annotated constructors and setter methods.

7.12 Java-based container configuration