依赖注入

声明

本文为其他博主原创文章整合,仅用作个人学习,特此声明

参考文章链接

(3条消息) B站 - 狂神 - Spring5课堂笔记_夜里的雨的博客-CSDN博客_狂神spring5笔记

6、依赖注入(DI)

6.1 构造器注入

第4点有提到,不再过多赘述

6.2 set方式注入【重点】

依赖注入:set注入!

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

  1. 复杂类型

    Address类

  2. 真实测试对象

    Student类

  3. beans.xml

  4. 测试

    MyTest3


1、测试代码

1.Student类

import java.util.*;
@Get
@Set
public class Student {
//别忘了写get和set方法(用lombok注解也行)
    private String name;
    private Address address;

    private String[] books;
    private List<String> hobbies;

    private Map<String, String> card;
    private Set<String> game;

    private Properties infor;
    private String wife;

    @Override
    public String toString() {
        return "Student{" +"\n"+
                "name='" + name + '\'' +"\n"+
                ", address=" + address.toString() +"\n"+
                ", books=" + Arrays.toString(books) +"\n"+
                ", hobbies=" + hobbies +"\n"+
                ", card=" + card +"\n"+
                ", game=" + game +"\n"+
                ", infor=" + infor +"\n"+
                ", wife='" + wife + '\'' +"\n"+
                '}';
    }
}

2.Address类

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 + '\'' +
            '}';
    }
}

3.beans.xml

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="address" class="com.xy.pojo.Address">
		<property name="address" value="address你好" />
	</bean>

	<bean id="student" class="com.xy.pojo.Student">
		<!--第一种,普通值注入-->
		<property name="name" value="name你好" />
		<!--第二种,ref注入-->
		<property name="address" ref="address" />

		<!--数组注入-->
		<property name="books">
			<array>
				<value>三国演义</value>
				<value>西游记</value>
				<value>水浒传</value>
			</array>
		</property>

		<!--list列表注入 -->
		<property name="hobbies">
			<list>
				<value></value>
				<value></value>
				<value>rap</value>
				<value>篮球</value>
			</list>
		</property>

		<!--map键值对注入-->
		<property name="card">
			<map>
				<entry key="username" value="root" />
				<entry key="password" value="root" />
			</map>
		</property>

		<!--set(可去重)注入-->
		<property name="game">
			<set>
				<value>我超,o!</value>
				<value>我超,撸!</value>
				<value>我超,农!</value>
			</set>
		</property>

		<!--空指针null注入(好惨,没有wife)-->
		<property name="wife">
			<null></null>
		</property>

		<!--properties常量注入-->
		<property name="infor">
			<props>
				<prop key="id">2017xxxxxx</prop>
				<prop key="name">xy</prop>
			</props>
		</property>
	</bean>
</beans>

4.测试类MyTest3

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;

public class MyTest3 {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		Student stu = (Student) context.getBean("student");
		System.out.println(stu.toString());
	}	
}

6.3 拓展注入

接下来我们练习一下这种注入方式

1.pojo增加User类

public class User {
    private String name;
    private int id;
	public User() {
        
	}
	public User(String name, int id) {
		super();
		this.name = name;
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", id=" + id + "]";
	}
}


2.注意

  • beans 里面加上这下面两行

    使用p和c命名空间需要导入xml约束

    xmlns:p=“http://www.springframework.org/schema/p”
    xmlns:c=“http://www.springframework.org/schema/c”
    
  • p命名空间注入/set注入,可以直接注入属性的值

  • c命名空间,通过构造器注入,需要写入有参和无参构造方法


3.beans.xml

?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入/set注入,可以直接注入属性的值-》property-->
    <bean id="user1" class="com.xy.pojo.User" p:name="cxk" p:id="20" >
    </bean>

    <!--c命名空间,通过构造器注入,需要写入有参和无参构造方法-》construct-args-->
    <bean id="user2" class="com.xy.pojo.User" c:name="cxk" c:id="22"></bean>
</beans>

4.测试

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user1",User.class);//确定class对象,就不用再强转了
System.out.println(user.toString());

6.4 Bean作用域

  • 单例模式(默认)

    <bean id="user2" class="com.xy.pojo.User" c:name="cxk" c:age="19" scope="singleton">
    </bean>
    

    单例模式是把对象放在pool中,需要再取出来,使用的都是同一个对象实例

    如下图,从同一个容器中取出来的对象是同一个!

  • 原型模式: 每次从容器中get的时候,都产生一个新对象

    <bean id="user2" class="com.xy.pojo.User" c:name="cxk" c:age="19" scope="prototype">
    </bean>
    

    如下图,同样是从user2容器中取出来的对象,每次都是新的!

  • 其余的request、session、application这些只能在web开发中使用!

posted @   无关风月7707  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示