Spring DI依赖注入之 set注入柒种方式,美滋滋 (二)

写在前面:
本文主要学习依赖注入之set注入的方式,如何注入Bean容器。
在环境搭建好的前提下进行创建pojo类。
DI 注入的讲解

DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。通过依赖注入机制,我们只需要通过简单的配置,而无需任何代码就可指定目标需要的资源,完成自身的业务逻辑,而不需要关心具体的资源来自何处,由谁实现。

理解DI的关键是:“谁依赖谁,为什么需要依赖,谁注入谁,注入了什么”,那我们来深入分析一下:

●谁依赖于谁:当然是应用程序依赖于IoC容器;

●为什么需要依赖:应用程序需要IoC容器来提供对象需要的外部资源;

●谁注入谁:很明显是IoC容器注入应用程序某个对象,应用程序依赖的对象;

●注入了什么:就是注入某个对象所需要的外部资源(包括对象、资源、常量数据)。


1.地址类:

public class Address {
// 引用类型
private String address;

public String getAddress() {
	return address;
}

public void setAddress(String address) {
	this.address = address;
}

@Override
public String toString() {
	return "Address{" +
			"address='" + address + '\'' +
			'}';
}
}

2.学生类:

public class Student {
private String name;
private Address address; // 唯一引用对象
private String[]Books;
private List<String> hobbs;
private Map<String,String>card;
private Set<String>games;
private String wife;
private Properties info;

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public Address getAddress() {
	return address;
}

public void setAddress(Address address) {
	this.address = address;
}

public String[] getBooks() {
	return Books;
}

public void setBooks(String[] books) {
	Books = books;
}

public List<String> getHobbs() {
	return hobbs;
}

public void setHobbs(List<String> hobbs) {
	this.hobbs = hobbs;
}

public Map<String, String> getCard() {
	return card;
}

public void setCard(Map<String, String> card) {
	this.card = card;
}

public Set<String> getGames() {
	return games;
}

public void setGames(Set<String> games) {
	this.games = games;
}

public String getWife() {
	return wife;
}

public void setWife(String wife) {
	this.wife = wife;
}

public Properties getInfo() {
	return info;
}

public void setInfo(Properties info) {
	this.info = info;
}

@Override
public String toString() {
	return "Student{" +
			"name='" + name + '\'' +
			", address=" + address +
			", Books=" + Arrays.toString(Books) +
			", hobbs=" + hobbs +
			", card=" + card +
			", games=" + games +
			", wife='" + wife + '\'' +
			", info=" + info +
			'}';
}
}

3.resources 目录下创建Applicationcountext.xml 目录,

创建使用NEW —>File 的方式

4.本章节的关键点:

<?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"
   xmlns:c="http://www.springframework.org/schema/c"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.ZQQQ.pojo.Address">
<property name="address" value="西安古都"/>
</bean>
	<bean id="Student" class="com.ZQQQ.pojo.Student">
	<!--第一种:普通值注入 -->
<property name="name" value="HEWEI"/>
	<!-- 第二种注入 bean注入 ref-->
		  <property name="address" ref="address"/>
    <!-- 第三种注入 数组注入 array-->
    <property name="books">
    <array>
        <value>四大名著</value>
        <value>红楼梦</value>
        <value>西游记</value>
        <value>水浒传</value>
        <value>三国演义</value>
    </array>
    </property>
    <!--第三种注入 list 注入 list-->
    <property name="hobbs">
        <list>
            <value>打篮球</value>
            <value>看电影</value>
            <value>敲代码</value>
            <value>狂街</value>
        </list>
    </property>
<!--第四种注入Map 注入 -->
<property name="card">
<map>
    <entry key="身份证" value="250369862014x"/>
    <entry key="手机号" value="13653629874"/>
</map>
</property>
 <!-- 第五种注入Set 注入-->
<property name="games">
  <set>
      <value>CF</value>
        <value>LOL</value>
         <value>COC</value>
         <value>BOB</value>
</set>
</property>
 <!--第六种注入null 值注入 -->
    <property name="wife">
      <null/>
    </property>
    <!-- 第七种注入Properties -->
    <property name="info">
        <props>
       <prop key="学号">20210410</prop>
            <prop key="姓名">贺伟</prop>
          <prop key="性别">男</prop>
        </props>
    </property>
   </bean>
  1. MyTest 测试用例:

public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“Applicationcontext.xml”);
Student student = (Student) context.getBean(“Student”);
System.out.println(student.toString());
}
}

  1. 输出结果
    在这里插入图片描述
总结:

1、前两种方式是我们经常使用的注入方式,剩下的List、Map、set、Properties、null为拓展注入的方式
2、拓展的5个为JavaEE集合章节中的知识 不过是换成标签元素进行注入的。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值