基于SpringBoot框架企业级应用系统开发全面实战(01.08_xml文件配置测试_recv.mp4)->{Demo1-11}
1、主题:
使用注解进行bean定义和依赖的内容;
第一步:
传统如何配置Bean并且完成Bean直接的依赖关系的建立。
建立三个类,Office ,Car,Boss 需要在Spring容器中配置为Bean;
Spring的IOC容器注入bean对象
一、基于XML配置bean方式
创建三个Java实体类用于测试
com.test.springoffice
Java实体类的set方法是SpringIOC注入,在使用XML配置bean时所必需。
Boss
package com.test.sprintoffice;
public class Boss {
private Car car;
private Office office;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Office getOffice() {
return office;
}
public void setOffice(Office office) {
this.office = office;
}
public String toString(){
return "car:"+car+"\n"+"office:"+office;
}
}
Car
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return "brand:"+brand+","+"price"+price;
}
}
Office
package com.test.sprintoffice;
public class Office {
private String officeNo="001";
public String getOfficeNo() {
return officeNo;
}
public void setOfficeNo(String officeNo) {
this.officeNo = officeNo;
}
public String toString(){
return "officeNo:"+officeNo;
}
}
bean.xml( applicationContext.xml )
元素 bean
标签 property
属性 name ref/value
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- boss类 -->
<bean id="boss" class="com.test.sprintoffice.Boss">
<property name="car" ref="car"/>
<property