【Spring】19 @Autowired注解使用详解


Spring 框架的 @Autowired 注解是实现依赖注入的一种强大而灵活的方式。在本文中,我们将介绍 @Autowired 注解的多种用法,包括构造函数、setter方法、字段、集合和特殊情况的处理。

构造函数注入

@Autowired 注解可以用于构造函数,例如:

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

从 Spring Framework 4.3 版本开始,如果目标 bean 的构造函数只有一个,就不再需要在该构造函数上添加@Autowired 注解。但如果存在多个构造函数,且没有主/默认构造函数,则至少要在其中一个构造函数上添加@Autowired 注解。

Setter方法注入

@Autowired 注解同样可以用于传统的 setter 方法:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

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

    // ...
}

此外,注解还可以用于具有任意名称和多个参数的方法:

public class MovieRecommender {

    private MovieCatalog movieCatalog;
    private CustomerPreferenceDao customerPreferenceDao;

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

    // ...
}

字段注入

@Autowired 注解还可以用于字段,并且可以与构造函数混用:

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    private MovieCatalog movieCatalog;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}

需要确保目标组件的声明类型与 @Autowired 注解的注入点类型一致,以避免在运行时出现 “no type match found” 错误。

数组和集合注入

通过 @Autowired 注解,你可以将容器中所有特定类型的 bean 注入到数组或集合中:

public class MovieRecommender {

    @Autowired
    private MovieCatalog[] movieCatalogs;

    // 或者

    private Set<MovieCatalog> movieCatalogs;

    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }

    // ...
}

如果希望数组或集合中的元素按照特定顺序排序,可以使用 @Order 注解或 Java 8 的 Optional、@Nullable 注解。

特殊情况处理

当没有匹配的候选 bean 可用于给定的注入点时,默认情况下,@Autowired 注解会导致注入失败。你可以通过将@Autowired 的 required 属性设置为 false 来更改此行为,使其变为非必需的:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired(required = false)
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

将 required 属性设置为 false 表示,如果无法进行自动装配,将跳过非必需的注入点,不会调用对应的方法或设置字段。

特殊接口类型的注入

@Autowired 注解还可以用于诸如 BeanFactory、ApplicationContext、Environment 等 Spring 容器内置的接口类型。这些接口和它们的扩展接口无需额外配置,Spring 会自动解析它们。

public class MovieRecommender {

    @Autowired
    private ApplicationContext context;

    // ...
}

异常处理

在实际应用中,如果无法匹配到候选 bean,@Autowired 注解将导致注入失败。对于构造函数和工厂方法参数的注入,required 属性有不同的含义。可以使用 Java 8 的 Optional、@Nullable 注解,或者在 Spring Framework 5.0 中使用 @Nullable 注解。

public class SimpleMovieLister {

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

    // 或者

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

结语

通过深入了解 @Autowired 注解的使用,你可以更灵活地进行依赖注入,提高代码的可读性和可维护性。选择合适的用法,可以使得 Spring 框架更好地服务于你的业务需求。

  • 30
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值