Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring @Autowired comments


May 14, 2021 Spring


Table of contents


When developing with Spring, there are two main ways to configure, one in xml and the other in java config. S pring technology itself is constantly evolving and changing, from the current springboot hot degree, java config application is more and more extensive, in the process of using java config, we inevitably have a variety of comments to deal with, of which, we should use the most comments should be @Autowired comments. The function of this comment is to inject us with a well-defined bean.

@Autowired is the role of comments?

@Autowired, this comment we use a lot, and now, I want to ask, what exactly does it do?

First of all, we look at the scope, in fact, this comment is a comment that belongs to the container configuration of Spring, and the comments that belong to the container configuration of it are: @Required, @Primary, @Qualifier, and so on. Therefore@Autowired comment is a comment for the container configuration.

Second, we can literally say that the @autowired from the English word autowire, which means auto-assembled. W hat does automatic assembly mean? T he term originally meant that some industries used machines instead of people to automatically complete some of the assembly tasks that needed to be completed, or some other tasks. In the World of Spring, auto-assembly refers to the use of the bean in the Spring container to automatically assemble the class we need.

@Autowired comment usage

Before analyzing how this comment works, let's review the use @Autowired the comment.

Apply @Autowired comments to constructors, as shown in the following example

public class MovieRecommender {
 
    private final CustomerPreferenceDao customerPreferenceDao;
 
    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }
 
    // ...
}

Apply @Autowired comments to the setter method

public class SimpleMovieLister {
 
    private MovieFinder movieFinder;
 
    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
 
    // ...
}

Apply @Autowired comments to methods that have any name and multiple parameters

public class MovieRecommender {
 
    private MovieCatalog movieCatalog;
 
    private CustomerPreferenceDao customerPreferenceDao;
 
    @Autowired
    public void prepare(MovieCatalog movieCatalog,
            CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }
 
    // ...
}

You can @Autowired comments to fields, or mix them with constructors, as shown in the following example

public class MovieRecommender {
 
    private final CustomerPreferenceDao customerPreferenceDao;
 
    @Autowired
    private MovieCatalog movieCatalog;
 
    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }
 
    // ...
}

Applying directly to a field is one of the most commonly used methods we use, but using construction method injection is better at the code level. In addition, there are a few less common ways

Adding @Autowired comments to a field or method that requires an array of that type, Spring searches ApplicationContext for all beans that match the specified type, as in the following example:

public class MovieRecommender {
 
    @Autowired
    private MovieCatalog[] movieCatalogs;
 
    // ...
}

Arrays can, we can immediately cite three, that container can also, the answer is yes, here is the example of set and map:

public class MovieRecommender {
 
    private Set<MovieCatalog> movieCatalogs;
 
    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }
 
    // ...
}
public class MovieRecommender {
 
    private Map<String, MovieCatalog> movieCatalogs;
 
    @Autowired
    public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }
 
    // ...
}

These are @Autowired use of spring comments, and frequent spring words should be unfamiliar with some of them.