系列七、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=研发部))
相关推荐
codeRichLife23 分钟前
Mybatisplus3.5.6,用String处理数据库列为JSONB字段
java·数据库
来自星星的猫教授29 分钟前
Java 文件注释规范(便于生成项目文档)
java·注释
zhimeng333 分钟前
自己学习原理
java
程序员鱼皮36 分钟前
学 Java 还是 Go 语言?这事儿很简单!
java·后端·计算机·程序员·开发·编程经验·自学编程
Lanqing_076040 分钟前
淘宝商品详情图API接口返回参数说明
java·服务器·前端·api·电商
矮油0_o1 小时前
第一部分 -- ①语法分析的概要
java·编译器·解释器·语法分析
写bug写bug1 小时前
Dubbo中SPI机制的实现原理和优势
java·后端·dubbo
浮游本尊1 小时前
第2天Java学习作业 - 完整解答
java
一叶萩Charles1 小时前
线程与进程(java)
java·开发语言
武昌库里写JAVA1 小时前
iview组件库:当后台返回到的数据与使用官网组件指定的字段不匹配时,进行修改某个属性名再将response数据渲染到页面上的处理
java·开发语言·spring boot·学习·课程设计