当前位置: 首页 > news >正文

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

一、概述

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

二、分类

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

2.1、byName

@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

@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=研发部))

http://www.lryc.cn/news/132683.html

相关文章:

  • 01- vdom 和模板编译源码
  • C++入门知识点——解决C语言不足
  • 探秘分布式大数据:融合专业洞见,燃起趣味火花,启迪玄幻思维
  • 什么是 SPI,和API有什么区别?
  • python3 安装clickhouse_sqlalchemy(greenlet) 失败
  • 五款拿来就能用的炫酷表白代码
  • Springboot 封装整活 Mybatis 动态查询条件SQL自动组装拼接
  • 宝塔部署Java+Vue前后端分离项目经验总结
  • 【公告】停止更新
  • AutoHotKey+VSCode开发扩展推荐
  • 了解 JSON 格式
  • [RDMA] 高性能异步的消息传递和RPC :Accelio
  • typescript报错:‘name‘ was also declared here
  • 第十章:联邦学习视觉案例
  • c语言——输出一个整数的所有因数
  • mqtt学习记录
  • 爬虫逆向实战(十八)--某得科技登录
  • Java-数组
  • Dart 入门Hello world
  • HTML是什么?
  • 【UniApp开发小程序】商品详情展示+评论、评论展示、评论点赞+商品收藏【后端基于若依管理系统开发】
  • rabbitMq安装后无法启动可视化页面http://localhost:15672处理
  • 材料行业可以转IC设计后端吗?
  • vue3 基础知识
  • 【线性代数-3Blue1Brown】- 2 线性组合、张成的空间与基
  • Kafka—工作流程、如何保证消息可靠性
  • 用户参与策略:商城小程序的搭建与营销
  • 可自定义实时监控系统HertzBeat
  • 无涯教程-Perl - sysread函数
  • Redis数据结构之String