Spring @Autowired注解实例

x33g5p2x  于2022-10-06 转载在 Spring  
字(2.5k)|赞(0)|评价(0)|浏览(758)

在这篇文章中,我们将讨论一个非常重要的Spring依赖注入注解,即@Autowired注解。

我们可以使用@Autowired来标记Spring将要解析和注入的依赖关系。我们可以在构造器设置器注入中使用这个注解。
下图显示了@Autowired注解的内部实现。

构造函数注入

  1. @RestController
  2. public class CustomerController {
  3. private CustomerService customerService;
  4. @Autowired
  5. public CustomerController(CustomerService customerService) {
  6. this.customerService = customerService;
  7. }
  8. }

设置器注入

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RestController;
  3. @RestController
  4. public class CustomerController {
  5. private CustomerService customerService;
  6. @Autowired
  7. public void setCustomerService(CustomerService customerService) {
  8. this.customerService = customerService;
  9. }
  10. }

字段注入

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RestController;
  3. @RestController
  4. public class CustomerController {
  5. @Autowired
  6. private CustomerService customerService;
  7. }

在不同的层次上应用@Autowired注解

@Autowired注解可以应用于不同的层次或场景。下面是一些相同的例子。

**例子:**将注解应用于具有任意名称和/或多个参数的方法。

  1. public class MovieRecommender {
  2. private MovieCatalog movieCatalog;
  3. private CustomerPreferenceDao customerPreferenceDao;
  4. @Autowired
  5. public void prepare(MovieCatalog movieCatalog,
  6. CustomerPreferenceDao customerPreferenceDao) {
  7. this.movieCatalog = movieCatalog;
  8. this.customerPreferenceDao = customerPreferenceDao;
  9. }
  10. // ...
  11. }

**示例:**将@Autowired也应用于字段,甚至与构造函数混合。

  1. public class MovieRecommender {
  2. private final CustomerPreferenceDao customerPreferenceDao;
  3. @Autowired
  4. private MovieCatalog movieCatalog;
  5. @Autowired
  6. public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
  7. this.customerPreferenceDao = customerPreferenceDao;
  8. }
  9. // ...
  10. }

**示例:**将@Autowired注解应用于期望有该类型数组的字段或方法。

  1. public class MovieRecommender {
  2. @Autowired
  3. private MovieCatalog[] movieCatalogs;
  4. // ...
  5. }

**示例:**将@Autowired注解应用于同样适用于类型的集合。

  1. public class MovieRecommender {
  2. private Set<MovieCatalog> movieCatalogs;
  3. @Autowired
  4. public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
  5. this.movieCatalogs = movieCatalogs;
  6. }
  7. // ...
  8. }

你可以通过Java 8的java.util.Optional来表达特定依赖的非必需性质。

  1. public class SimpleMovieLister {
  2. @Autowired
  3. public void setMovieFinder(Optional<MovieFinder> movieFinder) {
  4. ...
  5. }
  6. }

对于众所周知的可解析依赖的接口,我们也可以使用@AutowiredBeanFactory, ApplicationContext, Environment, ResourceLoader, ApplicationEventPublisher, and MessageSource

  1. public class MovieRecommender {
  2. @Autowired
  3. private ApplicationContext context;
  4. public MovieRecommender() {
  5. }
  6. // ...
  7. }

@Autowired 注解可选元素

  • boolean required - 声明注解的依赖关系是否是必需的。例子。
  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. @Autowired(required = false)
  4. public void setMovieFinder(MovieFinder movieFinder) {
  5. this.movieFinder = movieFinder;
  6. }
  7. // ...
  8. }

相关文章