spring学习(四)——spring官方文档阅读(5.0.7)——自动装配

本文详细介绍了Spring框架中的自动装配技术,包括自动装配的不同模式(如byName、byType、constructor等),并解释了如何在XML配置文件及使用注解的方式中应用自动装配。此外,还探讨了自动装配的局限性及其解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自动装配

上面的方式都是我们自己配置所需要的依赖,需要依赖的id或是name,我们也可以使用spring的自动装配技术,只需简单一步,轻松、快捷、方便,居家旅行,值得拥有........,容器会寻找我们所需要的bean,不一定需要我们指定依赖的id或name(用起来真的很爽呀!!!),而且自动装配很方便扩展,类多一个属性时,可以 不需要更改构造函数,用起来真的爽,对于xml来说,可以在<bean/>中指定autowire属性,该属性可取的值如下:

no(默认值):不需要自动装配

byName:通过属性名自动装配,依赖的名字和属性的名字一致,例如属性名为master,则spring查找容器中名为master的bean进行依赖注入(通过setter方法注入依赖)

byType:通过类型自动装配,如果有多个匹配(如果我们的属性类型使用的是接口,可能就会有多个匹配),则会出现异常,如果没有哪个bean匹配,则会默认置为null(有点坑)(通过setter方法注入依赖)

constructor:和byType原理一样,使用类型自动装配,但自动装配仅限于构造函数的参数,如果没有哪个bean满足,则会抛出异常(这点与byType不同)(通过构造函数注入依赖)

 

如果我们使用byType或是constructor模式,可以自动装配数组和集合,此时会扫描装配所有匹配类型的bean,对于map来说,当key是字符串时,可以自动装配,key为匹配的bean的id或是name名,value为匹配的bean

 

自动装配的局限性

1、不能装配基础类型(int、double等)、String、Class(以及值为这些类型的数组)

2、自动装配没有我们自己显示配置依赖精确

3、保证依赖解析具有唯一性的前提下(例如使用byType,我们必须保证匹配类型的bean只有一个),我们才能使用自动装配,否则会抛出异常

 

将某个bean的primary属性置为true,当有多个匹配依赖时,primary为true的bean将作为匹配结果,可以解决自动装配的第三点局限性

将某个bean的autowire-candidate属性置为false,自动装配匹配时,将忽略这个bean,这个属性只对值为byType的自动装配有效

<beans/>的default-autowire-candidate属性用于设置一个或多个匹配模式,例如我想匹配name为Repoeitory结尾的bean,可以在default autowired candidate中设置*Repository,autowired-candidate为false的bean不会参与匹配

 

通过xml进行自动装配:https://blog.youkuaiyun.com/trigl/article/details/50479795,该篇博客的autowire="autodetect "在文档中是没有提及的,实测也发现不存在

 

使用注解自动装配

@Autowired:用于setter函数、构造函数、普通函数表示自动装配形参,应用于属性,表示自动装配属性

想使用这个注解,首先要在xml的xsi:schemaLocation中添加"http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd",接着在<beans/>中添加<context:annotation-config/>,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
        >
<context:annotation-config/>
</beans>

应用于setter:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

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

    // ...
}

应用于构造函数:

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

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

    // ...
}

从spring 4.3开始,如果bean只有一个构造函数,可以不用使用@Autowired,如果有多个构造函数,必须对其中一个使用注解@Autowired或是进行xml配置来告诉容器用哪个构造函数初始化bean

 

应用于普通函数:

public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

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

    // ...
}

 

应用于属性,在实例化bean的时候就会注入依赖:

public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    private MovieCatalog movieCatalog;

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

    // ...
}

 

 

对于工厂方法来说,如果返回的类继承了许多接口,工厂方法return的类型应该尽可能和自动装配的类型匹配,防止自动装配失败

 

@Autowired可以用于数组的自动装配,此时所有类型满足的bean都会加入到数组中:

public class MovieRecommender {

    @Autowired
    private MovieCatalog[] movieCatalogs;

    // ...
}

@Autowired可以用于collections的自动装配,此时所有类型满足的bean都会加入到collections中:

public class MovieRecommender {

    private Set<MovieCatalog> movieCatalogs;

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

    // ...
}

 

bean可以通过继承org.springframework.core.Ordered接口或是使用@Order或@Priority指明bean加载顺序(通过这种方式,我们可以控制数组或是链表中bean的顺序),否则,bean之间的优先级通过它们的注册顺序决定,@Order可以标注在类上,也可以标注在用@Bean标注的方法上,@Priority不能用于@Bean注释的方法

 

如果Map的key为String,可以进行自动装配,此时key将是bean的name或id:

public class MovieRecommender {

    private Map<String, MovieCatalog> movieCatalogs;

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

    // ...
}

默认情况下,@Autowired注解的属性必须有一个bean可以注入,如果没有,则会抛出异常,可以通过下列方式更改此行为:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

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

    // ...
}

当@Autowired的required属性为true时,效果和@required一样

对于java 8的java.util.Optional类,也可以使用@Autowired注解,可以用@Nullable注解表明参数允许为空,与@Autowired配合相当于@Autowired(required=false):

public class SimpleMovieLister {

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

@Autowired还可用于许多众所周知的接口:BeanFactory、ApplicationContext、Environment、ResourceLoader、ApplicationEventPublisher、MessagerSource,以及上述这些接口的扩展接口:ConfigurableApplicationContext、ResourcePatternResolver

@Autowired、@Inject、@Resource、@Value注解由spring实现的BeanPostProcessor处理,@Autowired是基于类型进行自动装配的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值