手把手教你使用 Spring IOC 容器完成注入操作(xml注入 + 注解注入)(1)

// =============== 划重点 ===============

// 1. 获取核心容器对象

ApplicationContext ac = new ClassPathXmlApplicationContext(“bean.xml”);

// ApplicationContext ac = new FileSystemXmlApplicationContext(“xxx”); // 这里填写配置文件,在你本机上的物理地址,很少用户

// 2. 根据 id 获取 Bean 对象 (方式一)

IAccountService as = (IAccountService) ac.getBean(“accountService”);

System.out.println(as);

as.saveAccount();

//2. 根据 id 获取 Bean 对象 (方式二)

// IAccountDao adao = ac.getBean(“accountDao”, IAccountDao.class);

// as.saveAccount();

// System.out.println(adao);

}

}

运行结果:

我们没有使用 传统的方式,接用 Spring 框架完成了 bean 的实例化

在这里插入图片描述

2.2 使用 setter 完成注入

2.2.1 使用 setter 完成依赖注入的功能

涉及的标签:property

出现的位置:bean 标签的内部

标签的属性:

name:用于指定注入时所用的 set 方法名称

== 以上三个用于指定给构造函数中哪个参数赋值 ==

value: 用于给基本类型和 String类型的数据

ref:用于指定其它的 bean 类型数据,它指的就是 spring IOC 核心容器中出现过的 bean 对象

2.2.2 基于 setter 完成依赖注入的分析
  • 优势:

创建对象时没有明确的限制,可以直接使用默认构造函数

  • 弊端:

如果某个成员必须有值,则获取对象可能 set 方法没有执行

有了前面的内容做铺垫,接下来做 setter 注入就会轻松很多,我们需要做如下步骤

  1. 在 bean.xml 添加依赖

  1. 编写 IAccountServiceImpl2

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.Date;

/**

  • setter 注入

  • */

public class IAccountServiceImpl2 implements IAccountService {

private String name;

private Integer age;

private Date birthday;

public void setName(String name) {

this.name = name;

}

public void setAge(Integer age) {

this.age = age;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

@Override

public String toString() {

return “IAccountServiceImpl2{” +

“name='” + name + ‘’’ +

“, age=” + age +

“, birthday=” + birthday +

‘}’;

}

public void saveAccount() {

System.out.println(“service 中的 saveAccount 方法执行了”);

}

}

  1. Client 内容修改

package com.itheima.client;

import com.itheima.service.IAccountService;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

  • 模拟一个表现层,用于调用业务层

  • */

public class Client {

public static void main(String[] args) {

// 1. 获取核心容器对象

ApplicationContext ac = new ClassPathXmlApplicationContext(“bean.xml”);

// 2. 根据 id 获取 Bean 对象 (两种方式)

IAccountService as = (IAccountService) ac.getBean(“accountService2”);

System.out.println(as);

as.saveAccount();

}

}

  1. 效果图(数据成功通过 setter 注入)

在这里插入图片描述

2.2.3 基于 setter 注入的简化操作

约束新增 p 标签

<?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.xsd">

</

  • 28
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值