JSR-330 Standard Annotations

Source: Internet
Author: User

Starting with spring 3.0, spring begins to support annotations of the JSR-330 standard (dependency injection). These annotations and spring annotations are scanned in a way that developers only need to configure the relevant jar packages in classpath.

If a developer uses MAVEN to manage a project, javax.inject artifact is available in the Maven repository (http://repo1.maven.org/maven2/javax/inject/javax.inject/1/). Developers only need to refer to this dependency in the Pom.

<dependency>    <groupId>javax.inject</groupId>    <artifactId>javax.inject</artifactId>    <version>1</version></dependency>
Pass @InjectAnd @NamedMake Dependency Injection

In JSR-330, the @javax.inject.Inject same responsibilities as in spring @Autowired :

import javax.inject.Inject;public class SimpleMovieLister {    private MovieFinder movieFinder;    @Inject    public void setMovieFinder(MovieFinder movieFinder) {        this.movieFinder = movieFinder;    }    public void listMovies() {        this.movieFinder.findMovies(...);        ...    }}

and @Autowired consistent, developers can use @Inject dependency injection in instance variables, methods, and construction parameter levels. Also, developers can declare injections as Provider , by Provider.get() requesting those beans that are either short-scoped or deferred-initialized. For example, the following examples:

import javax.inject.Inject;import javax.inject.Provider;public class SimpleMovieLister {    private Provider<MovieFinder> movieFinder;    public void listMovies() {        this.movieFinder.get().findMovies(...);        ...    }}

If the developer wants to qualify the injected bean by name, you can use @Named annotations:

import javax.inject.Inject;import javax.inject.Named;publicclass SimpleMovieLister {    private MovieFinder movieFinder;    @Inject    publicvoidsetMovieFinder(@Named("main") MovieFinder movieFinder) {        this.movieFinder = movieFinder;    }    // ...}
@Named: Equivalent to @ComponentAnnotations

In JSR-330, the responsibilities in spring are similar to the @javax.inject.Named @Component following:

import javax.inject.Inject;import javax.inject.Named;@Named("movieListener")publicclass SimpleMovieLister {    private MovieFinder movieFinder;    @Inject    publicvoidsetMovieFinder(MovieFinder movieFinder) {        this.movieFinder = movieFinder;    }    // ...}

@ComponentOften do not need to specify the name of the use, the @Named annotations are the same:

import javax.inject.Inject;import javax.inject.Named;@Namedpublicclass SimpleMovieLister {    private MovieFinder movieFinder;    @Inject    publicvoidsetMovieFinder(MovieFinder movieFinder) {        this.movieFinder = movieFinder;    }    // ...}

@NamedYou can also use component scans when using annotations:

"org.example")public class AppConfig  {    ...}

Unlike spring's @Component components, JSR-330 Named annotations cannot be combined and use spring's component annotations if you want to customize component annotations.

Limitations of JSR-330 Standard annotations

When using JSR-330 standard annotations, it is also necessary to understand the different points of the spring annotations, as shown in the following table:

Spring javax.inject.* Javax.inject Restrictions
@Autowired @Inject @InjectAnnotations have no required attributes, but can be replaced by Java 8 Optional
@Component @Named The jsr_330 standard does not provide a composite model, there is only one way to identify the component
@Scope ("singleton") @Singleton JSR-330 The default scope is similar to spring, prototype However, why it is consistent with spring's default, beans in the JSR-330 standard are also singleton by default in spring. If you want to use a non-singleton scope, the developer should use spring @Scope annotations. java.injectan annotation is also provided @Scope , however, this annotation can only be used when creating a custom scope.
@Qualifier @Qualifier/@Named javax.inject.Qualifieris just a meta-annotation used to build a custom qualifier. A string qualifier (such as in spring @Qualifier ) can be used javax.inject.Named to implement the
@Value - Unequal price
@Required - Unequal price
@Lazy - Unequal price
Objectfactory Provider javax.inject.ProviderIs ObjectFactory another spring option, which get() Provider can be used in combination with spring by means of proxy. @Autowired

JSR-330 Standard Annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.