• Autowiring Dependencies


    Spring bean autowiring 定义:

    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.

    Enabling @Autowired Annotations

    @Autowired 的使用是有前置条件的 ☞ activate the dependency injection annotations

    Spring项目 ☞ 可选方式1

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

    @Configuration
    @ComponentScan("com.autowire.sample")
    public class AppConfig {}
    
    • 1
    • 2
    • 3

    Spring项目 ☞ 可选方式2

    The annotation is mainly used to activate the dependency injection annotations in Spring XML files.

    SpringBoot项目

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

    As a result, when we run this Spring Boot application, it will automatically scan the componentsin 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 <context:annotation-config> and <context:component-scan>
    Spring Component Scanning
    Spring @ComponentScan – Filter Types

    在这里插入图片描述

    There are four modes of autowiring a bean using an XML configuration

    1、 no: the default value – this means no autowiring is used for the bean and we have to explicitly name the dependencies.
    2、 byName: autowiring is done based on the name of the property, therefore Spring will look for a bean with the same name as the property that needs to be set.
    3、 byType: similar to the byName autowiring, only based on the type of the property. This means Spring will look for a bean with the same type of the property to set. If there’s more than one bean of that type, the framework throws an exception.
    3、constructor: autowiring is done based on constructor arguments, meaning Spring will look for beans with the same type as the constructor arguments.

    by type

    @Bean(autowire = Autowire.BY_TYPE)
    public class Store {
        private Item item;
        public setItem(Item item){
            this.item = item;    
        }
    }
    
    public class Store {
        @Autowired
        private Item item;
    }
    
    <bean id="store" class="org.store.Store" autowire="byType"> </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    By name

    # more than one bean of the same type,use the @Qualifier to reference a bean by name
    public class Store {
        @Autowired
        @Qualifier("item1")
        private Item item;
    }
    
    <bean id="item" class="org.store.ItemImpl1" />
    <bean id="store" class="org.store.Store" autowire="byName"> </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    We can also override the autowiring by defining dependencies explicitly through constructor arguments or setters.

    参考:Intro to Inversion of Control and Dependency Injection with Spring

  • 相关阅读:
    web3.js 基础
    民国漫画杂志《时代漫画》第34期.PDF
    MES系统物料管理的五大功能,建议收藏
    vue2 编写自己的组件库,并发布到npm
    PYTHON快捷键合集!学会让你成为大一最靓的仔
    CVE-2021-40449 NtGdiResetDC UAF
    基于单片机的智能电机保护器设计
    git使用
    NIO原理浅析(二)
    java计算机毕业设计课程在线反馈系统源码+数据库+lw文档+系统
  • 原文地址:https://blog.csdn.net/weixin_37646636/article/details/133325488