博客
关于我
spring的自动装配
阅读量:188 次
发布时间:2019-02-28

本文共 1798 字,大约阅读时间需要 5 分钟。

Spring自动装配详解

Spring框架提供了多种自动装配机制,帮助开发者简化依赖注入的配置。了解这些机制有助于更好地利用Spring的强大功能。

自动装配类型

Spring的自动装配机制主要有以下几种类型,开发者可以根据需求选择合适的方式:

  • byName:通过属性名寻找需要注入的对象。Spring会寻找与属性名相同的bean,如果找不到,则装不上。
  • byType:通过属性类型寻找需要注入的对象。Spring会寻找与属性类型匹配的bean,如果找不到或找到多个,会抛异常。
  • constructor:通过构造函数参数类型寻找需要注入的对象。Spring会寻找与构造函数参数类型匹配的一个或多个bean,如果找不到或找到多个,会抛异常。
  • autodetect:结合byType和constructor的优点,自动检测最佳匹配的bean。
  • default:指定默认的自动装配方式,需要在
    标签中配置。
  • no:不进行自动装配,是autowire默认值。
  • byName原理图

    理解byName原理图有助于更直观地掌握自动装配的工作流程。以下是简单的步骤说明:

    • Spring容器初始化时,解析XML配置文件。
    • 遍历所有bean标签。
    • 查找与属性名匹配的bean。
    • 如果找到且符合条件,则注入目标对象。

    实际案例

    Dog类

    package com.hsp.autowire;public class Dog {    private String name;    private int age;    public String getName() { return name; }    public void setName(String name) { this.name = name; }    public int getAge() { return age; }    public void setAge(int age) { this.age = age; }}

    Master类

    package com.hsp.autowire;public class Master {    private String name;    private Dog dog;    public String getName() { return name; }    public void setName(String name) { this.name = name; }    public Dog getDog() { return dog; }    public void setDog(Dog dog) { this.dog = dog; }}

    beans.xml配置

    顺平

    App1测试类

    package com.hsp.autowire;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 {    public static void main(String[] args) {        ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/autowire/beans.xml");        Master master = (Master) ac.getBean("master");        System.out.println(master.getName() + " 养 " + master.getDog().getName());    }}

    测试结果

    运行App1类,输出结果如下:

    顺平 养 小黄

    总结

    通过以上配置和测试,可以清晰地看到Spring自动装配的实际效果。理解这些机制有助于更高效地进行依赖注入,提升项目开发效率。

    转载地址:http://ifej.baihongyu.com/

    你可能感兴趣的文章
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>