Spring基于XML配置方式注入bean对象和@Resource注解的使用

基于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 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康冕峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值