系列七、IOC操作bean管理(xml自动装配)

一、概述

自动装配是根据指定规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入。

二、分类

xml自动装配分为按照属性名称自动装配(byName)和按照属性类型自动装配(byType)。

2.1、byName

java 复制代码
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Department implements Serializable {

    /**
     * 部门名称
     */
    private String name;

}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee implements Serializable {

    /**
     * 员工名称
     */
    private String name;

    /**
     * 性别
     */
    private String gender;

    /**
     * 部门
     */
    private Department department;
}

<?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">

    <bean id="employee" class="org.star.entity.Employee" autowire="byName">
        <property name="name" value="李白"></property>
        <property name="gender" value="男"></property>
        <property name="department" ref="department"></property>
    </bean>

    <bean id="department" class="org.star.entity.Department">
        <property name="name" value="研发部"></property>
    </bean>

</beans>

/**
 * IOC操作bean管理(xml自动装配-byName)
 */
@Test
public void beanManagementTest12() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext12.xml");
	Employee employee = context.getBean("employee", Employee.class);
	System.out.println("byName employee = " + employee);
}
// 控制台打印结果
byName employee = Employee(name=李白, gender=男, department=Department(name=研发部))

2.2、byType

java 复制代码
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Department implements Serializable {

    /**
     * 部门名称
     */
    private String name;

}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee implements Serializable {

    /**
     * 员工名称
     */
    private String name;

    /**
     * 性别
     */
    private String gender;

    /**
     * 部门
     */
    private Department department;
}

<?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">

    <bean id="employee" class="org.star.entity.Employee" autowire="byType">
        <property name="name" value="李白"></property>
        <property name="gender" value="男"></property>
        <property name="department" ref="department"></property>
    </bean>

    <bean id="department" class="org.star.entity.Department">
        <property name="name" value="研发部"></property>
    </bean>

</beans>

/**
 * IOC操作bean管理(xml自动装配-byType)
 */
@Test
public void beanManagementTest13() {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext13.xml");
	Employee employee = context.getBean("employee", Employee.class);
	System.out.println("byType employee = " + employee);
}
// 控制台打印结果
byType employee = Employee(name=李白, gender=男, department=Department(name=研发部))
相关推荐
Unity官方开发者社区28 分钟前
《Cryptical Path》开发诀窍:像玩游戏一样开发一款类Rogue游戏
java·游戏·玩游戏
_星辰大海乀31 分钟前
表的设计、聚合函数
java·数据结构·数据库·sql·mysql·数据库开发
IT成长史40 分钟前
deepseek梳理java高级开发工程师微服务面试题-进阶版
java·spring cloud·微服务
zkmall1 小时前
Java + 鸿蒙双引擎:ZKmall开源商城如何定义下一代B2C商城技术标准?
java·开源·harmonyos
陌路物是人非1 小时前
uniapp取消浏览自动填充
java·服务器·uni-app
獨枭1 小时前
使用 163 邮箱实现 Spring Boot 邮箱验证码登录
java·spring boot·后端
伍六星1 小时前
maven和npm区别是什么
java·npm·maven
才知山高路远1 小时前
Java - Junit框架
java·junit·log4j
维基框架1 小时前
Spring Boot 封装 MinIO 工具
java·spring boot·后端
秋野酱1 小时前
基于javaweb的SpringBoot酒店管理系统设计与实现(源码+文档+部署讲解)
java·spring boot·后端