首页技术栈归档照片墙音乐日记随想收藏夹友链留言关于

5.bean的自动装入

写作时间:2026-07-08

5.bean的自动装入

自动装配就是Spring依赖bean的一种方式

Spring 会在上下文中寻找,并自动给bean装配属性

在Spring 中有三种装配的方式

autowire 的属性和作用

名称 说明
no 默认值,表示不使用自动装配,Bean 依赖必须通过 ref 元素定义。
byName 根据 Property 的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。
byType 根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。
constructor 类似于 byType,根据构造方法参数的数据类型,进行 byType 模式的自动装配。
autodetect(3.0版本不支持) 如果 Bean 中有默认的构造方法,则用 constructor 模式,否则用 byType 模式。

Byname自动装入

       <bean id="cat" class="com.yuan.entity.Cat"/>
       <bean id="dog" class="com.yuan.entity.Dog"/>
    <!--
    byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid !
    -->
       <bean id="people" class="com.yuan.entity.People" autowire="byName">
           <property name="name" value="小黑"/>
        </bean>

ByType自动装入

       <bean id="cat" class="com.yuan.entity.Cat"/>
       <bean id="dog" class="com.yuan.entity.Dog"/>
     <!--
      byName:会自动在容器上下文中查找,和自己对象属性相同佛日beanid !
      -->
    <bean id="people" class="com.yuan.entity.People" autowire="byType">
        <property name="name" value="小黑"/>
    </bean>

小结

byname的时候,需要保证所有的beanid唯一,并且这个bean需要和自动注入的属性set方法的值一致!

byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性类型一致!

使用注解自动装配

使用注解须知

  1. 导入约束 context约束:
 xmlns:context="http://www.springframework.org/schema/context"

  1. 配置注解支持【重要】
 <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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--开起注解支持-->
    <context:annotation-config/>
</beans>

@Autowired

直接在属性是使用即可!也可在set方式上使用

使用Autowired我们可以不用编写set方法了,前提是你的这个自动装入的属性在IOC(Spring)容器中存在,符合byname!

科普

    @Nullable 字段标记了这个注解表示可以为null
    //如果显示定义了Autowired f的required属性为alse,说明这个对象可以为nuLl,否则不允许为空
    @Autowired(required = false)
    private Dog dog;
    @Autowired
    private  Cat cat;
    private String name;

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value="xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入!

public class People {
    //如果显示定义了Autowired f的required属性为alse,说明这个对象可以为nuLl,否则不允许为空
    @Autowired(required = false)
    private Dog dog;
    @Autowired
    @Qualifier(value="cat2")
    private  Cat cat;
    private String name;
}

@Resource注解

public class People {   
    @Resource(name = "cat")
    private  Cat cat;
}

@Resource和@Autowired的区别

都是用来自动装配的,都可以放在属性字段上!

@Autowired通过byType的方式实现,而且必须要去这个字段存在!【常用】

@Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个的找不到的情况就会报错!【常用】

执行顺序不同:@Autowired通过byType的方式实现。@Resource默认通过byname的方式实现

avatar

yuanyourdomain

写代码,做研究,记录生活。

RECOMMENDED

MyBatis 动态 SQL

2026-07-08

Nginx 基础入门

2026-07-08

Maven 多模块与私服

2026-07-08