系列七、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=研发部))
相关推荐
洛小豆2 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学2 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole2 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端
华仔啊2 小时前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端
程序员鱼皮3 小时前
刚刚 Java 25 炸裂发布!让 Java 再次伟大
java·javascript·计算机·程序员·编程·开发·代码
浮游本尊3 小时前
Java学习第21天 - 微服务架构设计
java
渣哥3 小时前
Java CyclicBarrier 详解:原理、使用方式与应用场景
java
杨杨杨大侠3 小时前
打开 JVM 黑匣子——走进 Java 字节码(一)
java·jvm·agent
SimonKing3 小时前
接口调用总失败?试试Spring官方重试框架Spring-Retry
java·后端·程序员