# 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.

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 @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 setMovieFinder(MovieFinder movieFinder) {
          this.movieFinder = movieFinder;
      }
      // ...
    }

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 {

      private MovieFinder movieFinder;

      @Inject
      public void setMovieFinder(@Named("main") MovieFinder movieFinder) {
          this.movieFinder = movieFinder;
      }
      // ...
    }

## @Named: a standard equivalent to the @Component 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 | 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 is only intended to be used for creating your own annotations.
@Qualifier | @Named | —
@Value | — | no equivalent
@Required | — | no equivalent
@Lazy | — | no equivalent