0
点赞
收藏
分享

微信扫一扫

Guide to Spring @Autowired 走近Spring的@Autowired注解

洒在心头的阳光 2022-02-17 阅读 136
springjava

原文链接: Guide to Spring @Autowired

1. Overview 概述

Starting with Spring 2.5, the framework introduced annotations-driven Dependency Injection. The main annotation of this feature is @Autowired. It allows Spring to resolve and inject collaborating beans into our bean.

从Spring的2.5版本开始,引入了基于注解的依赖注入。这个新功能的主要注解就是@Autowired注解,它允许Spring框架解析它所标记的依赖,并把它注入到我们的bean对象中。

In this tutorial, we'll first take a look at how to enable autowiring and the various ways to autowire beans. Afterward, we'll talk about resolving bean conflicts using @Qualifier annotation, as well as potential exception scenarios.

这篇文章中,首先看一下如何启用自动注入,并查看有几种方式来注入依赖bean。稍后,将介绍如何使用@Qualifier注解来解决bean对象冲突,还有一些可能出现异常的场景。

2. Enabling @Autowired Annotations 启用@Autowired注解功能

The Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.

Spring框架默认启动了自动依赖注入,换句话说,通过在配置文件中声明bean的依赖,Spring容器会自动关联声明了引用关系的bean对象,进行依赖注入,这就是Spring的自动注入。

To use Java-based configuration in our application, let's enable annotation-driven injection to load our Spring configuration:

要使用Java类进行配置,那么要启用基于注解的注入功能来加载Spring配置,示例如下:

@Configuration
@ComponentScan("com.baeldung.autowire.sample")
public class AppConfig {}

Alternatively, the <context:annotation-config> annotation is mainly used to activate the dependency injection annotations in Spring XML files.

或者,在XML文件中可以使用<context:annotion-config>来标记启用注解注入功能,这个标签也是最常用的启动这一功能的标签。

Moreover, Spring Boot introduces the @SpringBootApplication annotation. This single annotation is equivalent to using @Configuration@EnableAutoConfiguration, and @ComponentScan.

还有就是,SpringBoot 引入了 @SpringBootApplication 注解,它相当于同时启用了 @Configuration @EnableAutoConfiguration @ComponentScan 这三个注解。

Let's use this annotation in the main class of the application:

下面是@SpringBootApplication注解的使用示例:

@SpringBootApplication
class VehicleFactoryApplication {
    public static void main(String[] args) {
        SpringApplication.run(VehicleFactoryApplication.class, args);
    }
}

As a result, when we run this Spring Boot application, it will automatically scan the components in the current package and its sub-packages. Thus it will register them in Spring's Application Context, and allow us to inject beans using @Autowired.

按照上面代码进行编写,当我们启动它,会自动扫描当前包以及子包中的所有类,并在Spring程序上下文中进行注册,之后就可以作用于@Autowired注解标记的依赖,进行注入了。

3. Using @Autowired 使用@Autowired注解

After enabling annotation injection, we can use autowiring on properties, setters, and constructors.

启用了基于注解进行依赖注入的功能后,我们可以在字段、set方法及构造函数上进行使用

3.1. @Autowired on Properties 在字段上使用@Autowired注解

Let’s see how we can annotate a property using @Autowired. This eliminates the need for getters and setters.

来看一下如何在字段上使用@Autowired注解,这种方式可以省略字段所需的get/set方法

First, let's define a fooFormatter bean:

首先,定义一个bean对象,示例如下:

@Component("fooFormatter")
public class FooFormatter {
    public String format() {
        return "foo";
    }
}

Then, we'll inject this bean into the FooService bean using @Autowired on the field definition:

接着,把它注入到使用了@Autowired注解标记的字段上,当然,类型是相同的,示例如下:

@Component
public class FooService {  
    @Autowired
    private FooFormatter fooFormatter;
}

As a result, Spring injects fooFormatter when FooService is created.

结果就是,Spring框架正确处理了这个依赖注入,是在bean对象被创建时进行注入的。

3.2. @Autowired on Setters 在set方法上使用@Autowired注解

Now let's try adding @Autowired annotation on a setter method.

现在,试着在set方法上使用@Autowired注解

In the following example, the setter method is called with the instance of FooFormatter when FooServiceis created:

在下面的代码中,set方法在bean对象被创建的时个被调用,以此来注入所需的依赖

public class FooService {
    private FooFormatter fooFormatter;
    @Autowired
    public void setFooFormatter(FooFormatter fooFormatter) {
        this.fooFormatter = fooFormatter;
    }
}

3.3. @Autowired on Constructors 在构造函数上使用@Autowired注解

Finally, let's use @Autowired on a constructor.

最后,在构造函数上使用@Autowired注解

We'll see that an instance of FooFormatter is injected by Spring as an argument to the FooServiceconstructor:

从下面代码中,可以看到,这种写法,即可把FooFormatter对象被当做参数注入到依赖它的bean对象中

public class FooService {
    private FooFormatter fooFormatter;
    @Autowired
    public FooService(FooFormatter fooFormatter) {
        this.fooFormatter = fooFormatter;
    }
}

 

4. @Autowired and Optional Dependencies @Autowired注解与可选依赖

When a bean is being constructed, the @Autowired dependencies should be available. Otherwise, if Spring cannot resolve a bean for wiring, it will throw an exception.

在bean对象被构建时,所需的、被@Autowired注解标记的依赖bean应该已经被初始化完成,否则的话,Spring找不到依赖的对象时,就会抛出异常。

Consequently, it prevents the Spring container from launching successfully with an exception of the form:

因此,出现异常就会让Spring容器加载失败,会出现下面的错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.autowire.sample.FooDAO] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

To fix this, we need to declare a bean of the required type:

为也避免上面的异常,可以在@Autowired注解中声明依赖需求的级别:

public class FooService {
    @Autowired(required = false)
    private FooDAO dataAccessor; 
}

5. Autowire Disambiguation 消除自动注入过程中的歧义现象

By default, Spring resolves @Autowired entries by type. If more than one bean of the same type is available in the container, the framework will throw a fatal exception.

默认情况下,Spring框架根据@Autowired标记对象的类型来完成注入。但若是容器中出现了同一类型的多个bean实例,就会出现异常,毕竟它也不知道应该使用哪一个,毕竟没有选择策略。

To resolve this conflict, we need to tell Spring explicitly which bean we want to inject.

为也解决上述问题,就需要明确告知Spring框架,哪一个bean对象都是所需的依赖。

5.1. Autowiring by @Qualifier 使用@Qualifier注解配合自动注入

For instance, let's see how we can use the ​​​​​​@Qualifier annotation to indicate the required bean.

通过举例,来阐明如何使用@Qualifier注解来指明所需的bean依赖。

First, we'll define 2 beans of type Formatter:

首先,定义Formatter类型的两个实例对象

@Component("fooFormatter")
public class FooFormatter implements Formatter {
    public String format() {
        return "foo";
    }
}
@Component("barFormatter")
public class BarFormatter implements Formatter {
    public String format() {
        return "bar";
    }
}

Now let's try to inject a Formatter bean into the FooService class:

接着,通过@Autowired注解使框架自动进行依赖注入

public class FooService {
    @Autowired
    private Formatter formatter;
}

In our example, there are two concrete implementations of Formatter available for the Spring container. As a result, Spring will throw a NoUniqueBeanDefinitionException exception when constructing the FooService:

这上面的示例中,在Spring容器中会有两个Formatter的实现类的实例,所以,Spring会在构造FooService时抛出一个NoUniqueBeanDEfinitionException异常

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type [com.autowire.sample.Formatter] is defined: 
expected single matching bean but found 2: barFormatter,fooFormatter

We can avoid this by narrowing the implementation using a @Qualifier annotation:

可以使用@Qualifier注解来限制使用的实例对象来避免这个问题,示例如下 

public class FooService {
    @Autowired
    @Qualifier("fooFormatter")
    private Formatter formatter;
}

When there are multiple beans of the same type, it's a good idea to use @Qualifier to avoid ambiguity.

当出现一个类型的多个bean对象时,使用@Qualifier注解是一个好主意来解决这种依赖不明确问题。 

Please note that the value of the @Qualifier annotation matches with the name declared in the @Component annotation of our FooFormatter implementation.

需要注意的是,@Qualifier注解中的bean名称,是组件在使用@Component注解标记时的名称。

5.2. Autowiring by Custom Qualifier 实现自定义的Qualifier限定注入

Spring also allows us to create our own custom @Qualifier annotation. To do so, we should provide the @Qualifier annotation with the definition:

Spring框架允许开发者创建自己的@Qualifier注解,实现方式就是,在自己的注解上使用@Qualifier注解标记,示例如下:

@Qualifier
@Target({
  ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface FormatterType {  
    String value();
}

Then we can use the FormatterType within various implementations to specify a custom value:

 之后就可以使用我们的自定义注解@FormatterType来指定实例名称:

@FormatterType("Foo")
@Component
public class FooFormatter implements Formatter {
    public String format() {
        return "foo";
    }
}
@FormatterType("Bar")
@Component
public class BarFormatter implements Formatter {
    public String format() {
        return "bar";
    }
}

Finally, our custom Qualifier annotation is ready to use for autowiring:

最后,可以使用用于自动依赖注入

@Component
public class FooService {  
    @Autowired
    @FormatterType("Foo")
    private Formatter formatter;
}

The value specified in the @Target meta-annotation restricts where to apply the qualifier, which in our example is fields, methods, types, and parameters.

在自定义注解的文件中,@Target注解用于限制自定义注解的使用范围,在上面示例中,可以用于字段、方法、java类及参数。

5.3. Autowiring by Name 使用变量名进行查找依赖并注入

Spring uses the bean's name as a default qualifier value. It will inspect the container and look for a bean with the exact name as the property to autowire it.

默认情况下,Spring框架会使用bean对象的名称做为实例对象名,并会检查引用了这个名称的bean进行依赖注入。

Hence, in our example, Spring matches the fooFormatter property name to the FooFormatterimplementation. Therefore, it injects that specific implementation when constructing FooService:

因此,在下面的例子中,Spring框架会找寻类型为FooFormatter类型的fooFormatter实例对象。因此,在构造FooService时,会在容器中查找fooFormatter实例并进行注入,找不到就抛异常。

public class FooService {
 @Autowired 
private Formatter fooFormatter; 
}

6. Conclusion 最后

In this article, we discussed autowiring and the different ways to use it. We also examined ways to solve two common autowiring exceptions caused by either a missing bean or an ambiguous bean injection.

这篇文章,认识了一下自动注入,还知道了使用它的几种方式。并且,晓得如何解决两种常见的自动依赖注入异常,一是缺少对应bean对象,另一个是不明确的bean对象引用。

关注点赞哈

举报

相关推荐

0 条评论