Spring 5 Chinese parsing core part – jsr330 standard annotation of IOC container

Time:2020-11-20
1.11 use jsr330 standard annotation

Starting with spring 3.0, spring provides support for jsr-330 standard annotations (dependency injection). These annotations are scanned in the same way as spring annotations. To use them, you need to rely on the relevant jar package in the classpath.

If you use maven, javax.inject The components are in the standard Maven repository( https://repo1.maven.org/maven… 。 You can add the following dependencies to your pom.xml :

<dependency>
 <groupId>javax.inject</groupId>
 <artifactId>javax.inject</artifactId>
 <version>1</version>
</dependency>
1.11.1 @Injectand@NamedDependency injection

You can use it@javax.inject.Injectreplace@Autowired, similar to the following example:

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(...);
        // ...
    }
}

similar@AutowiredAnnotations can be used at the field level, method level, and construction parameter level@Inject。 In addition, you can declare the injection point asProviderTo allow on-demand access to shorter beans, or through providersget()Call to delay access to other beans. The following example provides a variation of the front face 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 like to use qualified names for dependencies that should be injected, you should use@NamedNote, similar to 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;
    }

    // ...
}

similar@Autowired@InjectIt can also be usedjava.util.Optionalor@Nullable。 This is more applicable here because@InjectNo,requiredProperty. The following two examples show how to use it@Injectand@Nullable

public class SimpleMovieLister {

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

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

Reference code:com.liyong.ioccontainer.starter.XmlInjectAndNamedIocContainer

1.11.2 @Namedand@ManagedBean: equivalent to@Component

replace@ComponentYou can use@javax.inject.Namedorjavax.annotation.ManagedBean, similar to 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;
    }

    // ...
}

Use if no component name is specified@ComponentIt’s very common.@NamedIt can be used in a similar way, such as the following example:

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

@Named
public class SimpleMovieLister {

    private MovieFinder movieFinder;

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

    // ...
}

use@Namedor@ManagedBeanYou can use component scanning in exactly the same way as using spring annotations. This is similar to the following example:

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

And@ComponentCompared with jsr330@NamedAnd jsr-250ManagedBeanAnnotations cannot be combined. You should use spring’s stereotype model to build custom component annotations.

Reference code:com.liyong.ioccontainer.starter.XmlNamedAndManagerBeanIocContainer

1.11.3 limitations of jsr-330 standard notes

When you use standard annotations, you should know that some important features are not available, as shown in the following table:

Spring javax.inject.* javax.inject restrictions / comments
@Autowired @Inject @InjectNo,requiredProperty. Can be used in Java 8Optional
@Component @Named / @ManagedBean Jsr-330 does not provide a composable model, only a method to identify named components.
@Scope("singleton") @Singleton The default scope of jsr-330 is spring’sprototypeHowever, in order to maintain general default consistency with spring, the jsr-330 bean is declared as asingleton。 In order to use other scopes, you should use spring’s@ScopeNotes. javax.inject There is also a@ScopeNotes. However, this is only used to create your own annotations.
@Qualifier @Qualifier / @Named javax.inject.QualifierIs to build a custom qualifier meta annotationStringRestricted passagejavax.inject.Namedrelation. (similar to spring’s@QualifierValue)
@Value no equivalent
@Required no equivalent
@Lazy no equivalent
ObjectFactory Provider javax.inject.ProviderIt is a direct replacement of spring’s objectfactory. There is only oneget()Method name, which can also be used with spring’s@AutowiredIt can also be used in combination with uncommented constructors and setter methods.

author

Personally engaged in the financial industry, I have worked in Chongqing’s first-class technical team, such as yijifu, Sijian technology, a car Hailing platform, etc. at present, I am in charge of the construction of unified payment system in a bank. I have a strong interest in the financial industry. At the same time, it also practices big data, data storage, automatic integration and deployment, distributed microservices, responsive programming, artificial intelligence and other fields. At the same time, it is also keen to share technology and create official account and blog site.

Blog address: http://youngitman.tech

CSDN: https://blog.csdn.net/liyong1…

The official account of WeChat:

Spring 5 Chinese parsing core part - jsr330 standard annotation of IOC container

Technical exchange group:

Spring 5 Chinese parsing core part - jsr330 standard annotation of IOC container

Recommended Today

Summary of recent use of gin

Recently, a new project is developed by using gin. Some problems are encountered in the process. To sum up, as a note, I hope it can help you. Cross domain problems Middleware: func Cors() gin.HandlerFunc { return func(c *gin.Context) { //Here you can use * or the domain name you specify c.Header(“Access-Control-Allow-Origin”, “*”) //Allow header […]