目录

1.依赖注入之setter注入

2.依赖注入之构造器注入

3.特殊值处理

4.为类类型属性赋值

5.为数组类型属性赋值


1.依赖注入之setter注入

①创建学生类 Student

package com.atguigu.spring.pojo;

public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;

public Student() {
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}

②配置bean时为属性赋值

<bean id="student" class="com.atguigu.spring.pojo.Student">
<property name="id" value="1001"></property>
<property name="name" value="田"></property>
<property name="age" value="21"></property>
<property name="sex" value="男"></property>
</bean>

③测试

@org.junit.Test
public void testHelloWorld(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = ac.getBean(Student.class);
System.out.println(student);


}

【SSM框架】依赖注入_junit

2.依赖注入之构造器注入

①在Student类中添加有参构造

public Student(Integer id, String name, Integer age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}

②配置 bean

<bean id="student" class="com.atguigu.spring.pojo.Student">
<constructor-arg value="1002"></constructor-arg>
<constructor-arg value="王"></constructor-arg>
<constructor-arg value="22"></constructor-arg>
<constructor-arg value="女" name="sex"></constructor-arg>
</bean>

注意:

constructor-arg 标签还有两个属性可以进一步描述构造器参数:

  • index属性:指定参数所在位置的索引(从0开始)
  • name属性:指定参数名

③测试

@org.junit.Test
public void testHelloWorld(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = ac.getBean(Student.class);
System.out.println(student);


}

【SSM框架】依赖注入_junit_02

3.特殊值处理

①字面量赋值


什么是字面量?

int a = 10;

声明一个变量 a ,初始化为 10 ,此时 a 就不代表字母 a 了,而是作为一个变量的名字。当我们引用 a

的时候,我们实际上拿到的值是 10 。

而如果 a 是带引号的: 'a' ,那么它现在不是一个变量,它就是代表 a 这个字母本身,这就是字面

量。所以字面量没有引申含义,就是我们看到的这个数据本身。


<!-- 使用value属性给bean的属性赋值时,Spring会把value属性的值看做字面量 -->
<property name="name" value="张三"/>

null

<property name="name">
<null />
</property>

注意:

<property name="name" value="null"></property>

以上写法,为 name 所赋的值是字符串 null

xml 实体

<!-- 小于号在XML文档中用来定义标签的开始,不能随便使用 -->
<!-- 解决方案一:使用XML实体来代替 -->
<property name="expression" value="a < b"/>

CDATA

<property name="expression">
<!-- 解决方案二:使用CDATA节 -->
<!-- CDATA中的C代表Character,是文本、字符的含义,CDATA就表示纯文本数据 -->
<!-- XML解析器看到CDATA节就知道这里是纯文本,就不会当作XML标签或属性来解析 -->
<!-- 所以CDATA节中写什么符号都随意 -->
<value><![CDATA[a < b]]></value>
</property>

4.为类类型属性赋值

①创建班级类 Clazz

public class Clazz {
private Integer clazzId;
private String clazzName;

public Integer getClazzId() {
return clazzId;
}

public String getClazzName() {
return clazzName;
}

public void setClazzId(Integer clazzId) {
this.clazzId = clazzId;
}

public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}

@Override
public String toString() {
return "Clazz{" +
"clazzId=" + clazzId +
", clazzName='" + clazzName + '\'' +
'}';
}
}

②修改 Student

public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
private Clazz clazz;

public Student() {
}

public Student(Integer id, String name, Integer age, String sex, Clazz clazz) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.clazz = clazz;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public Clazz getClazz() {
return clazz;
}

public void setClazz(Clazz clazz) {
this.clazz = clazz;
}

@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", clazz=" + clazz +
'}';
}
}

③方式一:引用外部已声明的 bean

配置Clazz类型的bean:

<bean id="clazz" class="com.atguigu.spring.pojo.Clazz">
<property name="clazzId" value="1"></property>
<property name="clazzName" value="计算机科学与技术"></property>
</bean>

为Student中的clazz属性赋值:

<bean id="student" class="com.atguigu.spring.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="刘"></property>
<property name="age" value="31"></property>
<property name="sex" value="男"></property>
<!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
<property name="clazz" ref="clazz"></property>


</bean>

错误演示:

<bean id="student" class="com.atguigu.spring.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="刘"></property>
<property name="age" value="31"></property>
<property name="sex" value="男"></property>
<!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
<property name="clazz" value="clazz"></property>


</bean>

如果错把 ref 属性写成了 value 属性,会抛出异常:

                                                                                  

Caused by: java.lang.IllegalStateException:

Cannot convert value of type 'java.lang.String' to required type

'com.atguigu.spring.bean.Clazz' for property 'clazz': no matching editors or conversion

strategy found


意思是不能把 String 类型转换成我们要的 Clazz 类型,说明我们使用 value 属性时, Spring 只把这个属性看做一个普通的字符串,不会认为这是一个bean 的 id ,更不会根据它去找到 bean 来赋值

④方式二:内部 bean

<bean id="student" class="com.atguigu.spring.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="26"></property>
<property name="sex" value="女"></property>
<property name="clazz">
<!-- 在一个bean中再声明一个bean就是内部bean -->
<!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
<bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
<property name="clazzId" value="2"></property>
<property name="clazzName" value="软件工程"></property>
</bean>
</property>
</bean>

③方式三:级联属性赋值

<bean id="student" class="com.atguigu.spring.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="26"></property>
<property name="sex" value="女"></property>
<!-- 一定先引用某个bean为属性赋值,才可以使用级联方式更新属性 -->
<property name="clazz" ref="clazz"></property>
<property name="clazz.clazzId" value="3333"></property>
<property name="clazz.clazzName" value="最强王者班"></property>
</bean>

5.为数组类型属性赋值

①修改 Student

在 Student 类中添加以下代码:

private String[] hobbies;

public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}

public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", clazz=" + clazz +
", hobbies=" + Arrays.toString(hobbies) +
'}';
}

②配置 bean

<bean id="student" class="com.atguigu.spring.pojo.Student">
<property name="id" value="1004"></property>
<property name="name" value="赵六"></property>
<property name="age" value="26"></property>
<property name="sex" value="女"></property>

<property name="clazz">
<!-- 在一个bean中再声明一个bean就是内部bean -->
<!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
<bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
<property name="clazzId" value="2"></property>
<property name="clazzName" value="软件工程"></property>
</bean>
</property>

<property name="hobbies">
<array>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</array>
</property>
</bean>

14天学习训练营导师课程: 郑为中《Vue和SpringBoot打造假日旅社管理系统》