20191226 Spring official documents (Core 1.11)

1.11. JSR 330 using standard comments

Starting Spring 3.0, Spring provides support for JSR-330 standard annotation (DI) of. Scan scanning the same way with these comments Spring comment. To use them, you need to have the relevant jar in the class path.

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

1.11.1. Dependency injection is @Inject and @Named

In addition @Autowired, you can use @javax.inject.Inject, as follows:

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 as you can at the field level, method level and the level of use constructor parameters @Inject. In addition, you can declare the injection point Provider, in order to allow on-demand access scopes smaller bean, or by Provider.get()calling delayed access to other bean. The following examples provide a variant of the previous example:

import javax.inject.Inject;
import javax.inject.Provider;

public class SimpleMovieLister {

    private Provider<MovieFinder> movieFinder;

    @Inject
    public void setMovieFinder(Provider<MovieFinder> movieFinder) {
        this.movieFinder = movieFinder;
    }

    public void listMovies() {
        this.movieFinder.get().findMovies(...);
        // ...
    }
}

If you want to use dependency injection should define the name, use @Namedannotations, as shown in the following example:

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;
    }

    // ...
}

And the @Autowiredlike @Injectmay also java.util.Optionalor @Nullableused together. This is more applicable here, because @Inject no required attributes. The following example shows how to use @Inject and @Nullable:

public class SimpleMovieLister {

    @Inject
    public void setMovieFinder(Optional<MovieFinder> movieFinder) {
        // ...
    }
}

public class SimpleMovieLister {

    @Inject
    public void setMovieFinder(@Nullable MovieFinder movieFinder) {
        // ...
    }
}

1.11.2. @Named and @ManagedBean: @Component comments equivalent standard

You can use @javax.inject.Namedor javax.annotation.ManagedBeaninstead of @Component, as in the following example:

import javax.inject.Inject;
import javax.inject.Named;

@Named("movieListener")  // @ManagedBean("movieListener") could be used as well
public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Inject
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

@NamedYou can not specify the component name

When used @Namedor @ManagedBeancan be used when using annotations Spring exactly the same way using a scanning component, as shown in the following example:

@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig  {
    // ...
}

Compared with @Component, JSR-330 @Named and JSR-250 ManagedBean annotations are not combinable. You should be built custom components using Spring's stereotype annotations model.

1.11.3. Limitations JSR-330 standard annotation

Spring javax.inject.* javax.inject restrictions / comments
@Autowired @Inject @Inject not "required" property. It can be used with Java 8 Optional.
@Component @Named / @ManagedBean JSR-330 does not provide a combination model, there is provided a method of identifying only the named component.
@Scope("singleton") @Singleton Default scope Spring JSR-330 are similar to the prototype. However, in order to be consistent with the default value Default Spring, the default statement Spring container JSR-330 bean is singleton. In order to use outside of the scope of singleton, you should use Spring's @Scope comment. javax.inject also provides @Scope comment. However, this is only for creating your own comments.
@Qualifier @Qualifier / @Named javax.inject.Qualifier only qualifiers for building custom metadata annotations. Specific String qualifier (e.g., with the value of the Spring @Qualifier) ​​by javax.inject.Named association.
@Value - There is no equivalent
@Required - There is no equivalent
@Lazy - There is no equivalent
ObjectFactory Provider javax.inject.Provider is a direct alternative to Spring's ObjectFactory, just a short get () method name. It can also be used in conjunction with or annotations Spring @Autowired constructor and setter methods.

Guess you like

Origin www.cnblogs.com/huangwenjie/p/12104049.html