系列七、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=研发部))
相关推荐
10km13 分钟前
java:Apache Commons Configuration2占位符解析异常的正确解法:${prefix:name:-default}
java·apache·configuration2·变量插值·interpolation
customer0814 分钟前
【开源免费】基于SpringBoot+Vue.JS个人博客系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
灰色人生qwer21 分钟前
SpringBoot 项目配置日志输出
java·spring boot·后端
2301_7930698231 分钟前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
阿华的代码王国38 分钟前
【从0做项目】Java搜索引擎(6)& 正则表达式鲨疯了&优化正文解析
java·后端·搜索引擎·正则表达式·java项目·从0到1做项目
服务端相声演员38 分钟前
Oracle JDK、Open JDK zulu下载地址
java·开发语言
是姜姜啊!39 分钟前
java连接redis
java·redis
hhw19911240 分钟前
spring boot知识点5
java·数据库·spring boot
EQUINOX142 分钟前
lab4 CSAPP:Cachelab
java·后端·spring
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS打卡健康评测系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源