目录
1.基于XML管理bean
1.1 入门
1.加入依赖
<dependencies>
<!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version> </dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2.随便创一个类
package com.xiaoye.pojio;
public class helloSpring {
public void sayhello(){
System.out.println("hello,spring");
}
}
3. spring配置文件
配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理通过bean标签配置IOC容器所管理的bean
bean属性:
id:bean的唯一标识,不能重复
class:设置bean对应的对象的全类名
<!--
配置HelloWorld所对应的bean,
即将HelloWorld的对象交给Spring的IOC容器管理
通过bean标签配置IOC容器所管理的bean
属性:
id:bean的唯一标识,不能重复
class:设置bean对应的对象的全类名
-->
<bean id="helloWord" class="com.xiaoye.pojio.helloSpring"></bean>
4.测试类
public class helloWordtest {
@Test
public void testHello(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");//获取IOC容器
//获取IOC容器中的bean
helloSpring helloSpring = (com.xiaoye.pojio.helloSpring) ioc.getBean("helloWord");
helloSpring.sayhello();
}
}
5.图解
6.注意
Spring 底层默认通过反射技术调用组件类的无参构造器来创建组件对象,这一点需要注意。如果在需要无参构造器时,没有无参构造器,会抛出异常。
1.2 获取bean的三种方式
2.1 根据id获取
根据之前写的bean的id获取
public void testIOC(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring_ioc.xml");
//获取bean对象
Student studentOne1 = (Student) ioc.getBean("studentOne");
System.out.println(studentOne1);
}
2.2 根据id和类型
<bean id="studentOne" class="com.xiaoye.pojo.Student">
<!--
通过set注入
property:通过成员变量的set方法进行赋值
name:设置需要赋值的属性名
value:设置属性需要的值
-->
<property name="sid" value="1001"></property>
<property name="age" value="22"></property>
<property name="gender" value="男"></property>
<property name="sname" value="李四"></property>
</bean>
public void testIOC(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring_ioc.xml");
//获取bean对象
Student studentOne3 = ioc.getBean("studentOne", Student.class);
System.out.println(studentOne3);
}
2.3 根据类型
<bean id="studentTwo" class="com.xiaoye.pojo.Student">
<!--
通过set注入
property:通过成员变量的set方法进行赋值
name:设置需要赋值的属性名
value:设置属性需要的值
-->
<property name="sid" value="1001"></property>
<property name="age" value="22"></property>
<property name="gender" value="男"></property>
<property name="sname" value="李四"></property>
</bean>
public void testIOC(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring_ioc.xml");
//获取bean对象
Student studentOne2 = ioc.getBean(Student.class);
System.out.println(studentOne1);
}
注意:
1.当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个当IOC容器中一共配置了两个,会抛出异常。
2.如果组件类实现了接口,根据接口类型可以获取 bean ,但是bean必须是唯一的。
如果一个接口有多个实现类,这些实现类都配置了 bean,根据接口类型就不可以获取 bean
根据类型来获取bean时,在满足bean唯一性的前提下,其实只是看:『对象 instanceof 指定的类
型』的返回结果,只要返回的是true就可以认定为和类型匹配,能够获取到。
1.3 依赖注入
3.1 依赖注入之Setter注入
property标签:通过组件类的setXxx()方法给组件对象设置属性
name属性:指定属性名(这个属性名是getXxx()、setXxx()方法定义的,和成员变量无关)
value属性:指定属性值
<bean id="studentOne" class="com.xiaoye.pojo.Student"></bean>
<bean id="studentTwo" class="com.xiaoye.pojo.Student">
<!--
通过set注入
property:通过成员变量的set方法进行赋值
name:设置需要赋值的属性名
value:设置属性需要的值
-->
<property name="sid" value="1001"></property>
<property name="age" value="22"></property>
<property name="gender" value="男"></property>
<property name="sname" value="李四"></property>
</bean>
3.2 依赖注入之构造器注入
构造器注入
<!--
通过有参构造器注入
-->
<bean id="studentThere" class="com.xiaoye.pojo.Student">
<constructor-arg value="1002"></constructor-arg>
<constructor-arg value="张三"></constructor-arg>
<constructor-arg value="22"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
注意:
constructor-arg标签还有两个属性可以进一步描述构造器参数:
index属性:指定参数所在位置的索引(从0开始)
name属性:指定参数名
1.4 特殊值处理
-
字面量赋值
什么是字面量?
int a = 10;
声明一个变量a,初始化为10,此时a就不代表字母a了,而是作为一个变量的名字。当我们引用a的时候,我们实际上拿到的值是10。而如果a是带引号的:'a',那么它现在不是一个变量,它就是代表a这个字母本身,这就是字面量。所以字面量没有引申含义,就是我们看到的这个数据本身。
<!-- 使用value属性给bean的属性赋值时,Spring会把value属性的值看做字面量 -->
<property name="name" value="张三"/>
-
null值
为name所赋的值是字符串null
<property name="name"> <null /> </property> <!--或者--> <property name="name" value="null"></property>
-
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>
1.5 为类类型的属性赋值
在student类里引入class类类型的属性
public class Student implements Person {
private Integer sid;
private String sname;
private Integer age;
private String gender;
private Clazz clazz;
class类
public class Clazz {
private Integer cid;
private String cname;
private List<Student> students;
4.1 引用外部类
1.配置class类型的bean
<bean id="clazzOne" class="com.xiaoye.pojo.Clazz">
<property name="cid" value="11111"></property>
<property name="cname" value="14班"></property>
</bean>
为student中clazz属性赋值
ref属性:引用IOC容器中的某个bean的id(外部bean),将所对应的bean为属性赋值
<bean id="studentFive" class="com.xiaoye.pojo.Student">
<property name="gender" value="男"></property>
<property name="sname" value="赵六"></property>
<property name="sid" value="1004"></property>
<property name="age" value="22"></property>
<!--ref:引用IOC容器中的某个bean的id(外部bean),将所对应的bean为属性赋值 -->
<property name="clazz" ref="clazzOne"></property>
<bean/>
2.内部bean
在一个bean中再声明一个bean就是内部bean
内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性
<bean id="studentFive" class="com.xiaoye.pojo.Student">
<property name="gender" value="男"></property>
<property name="sname" value="赵六"></property>
<property name="sid" value="1004"></property>
<property name="age" value="22"></property>
<!--内部bean,只能在当前bean内部使用,不能通过IOC容器获取 -->
<property name="clazz">
<bean id="clazzInner" class="com.xiaoye.pojo.Clazz">
<property name="cid" value="123"></property>
<property name="cname" value="2班"></property>
</bean>
</property
</bean>
3.级联方式
一定先引用某个bean为属性赋值,才可以使用级联方式更新属性 .
不推荐使用,他其实是修改,要求属性必须有初始值
<bean id="studentFour" class="com.atguigu.spring.bean.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" ref="clazzOne"></property>
<property name="clazz.clazzId" value="3333"></property>
<property name="clazz.clazzName" value="最强王者班"></property>
</bean>
1.6 为数组类型赋值
在sutuden类里添加数组类型属性
public class Student implements Person {
private Integer sid;
private String sname;
private Integer age;
private String gender;
private Clazz clazz;
private String[] hobby;
用<array>标签为数组类型属性赋值
<bean id="studentFive" class="com.xiaoye.pojo.Student">
<property name="gender" value="男"></property>
<property name="sname" value="赵六"></property>
<property name="sid" value="1004"></property>
<property name="age" value="22"></property>
<!--ref:引用IOC容器中的某个bean的id(外部bean),将所对应的bean为属性赋值 -->
<property name="clazz" ref="clazzOne"></property>
<property name="hobby" >
<array>
<value>月亮</value>
<value>诗歌</value>
<value>爱情</value>
</array>
</property>
<bean/>
1.7 为集合类型属性赋值
1.为list集合赋值(1对多)
在class类添加集合类型属性
public class Clazz {
private Integer cid;
private String cname;
private List<Student> students;
1.1 使用list标签,并且使用ref引入外部bean完成赋值
<!--一对多,内部引用list标签 -->
<bean id="clazzTwo" class="com.xiaoye.pojo.Clazz">
<property name="cid" value="001"></property>
<property name="cname" value="12班"></property>
<property name="students" >
<list>
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThere"></ref>
</list>
</property>
1.2 使用list类型的bean,ref外部引用(注意需要引入相应的命名空间)
<?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:util="http://www.springframework.org/schema/util"
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 http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<
<!-- 使用list类型的bean,需要使用util的约束 -->
<util:list id="studentList">
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThere"></ref>
</util:list>
<bean id="clazzThere" class="com.xiaoye.pojo.Clazz">
<property name="cid" value="001"></property>
<property name="cname" value="11班"></property>
<property name="students" ref="studentList">
</property>
</bean>
2.为map类型集合赋值
创建教师类和map集合
public class Teacher {
private Integer tid;
private String tname;
public class Student implements Person {
private Integer sid;
private String sname;
private Integer age;
private String gender;
private Clazz clazz;
private String[] hobby;
private Map<String, Teacher> teacherMap;
2.1 先给teach类型的bean赋值,在使用<map>标签的<entry>标签的key属性设置键,用value-ref属性引用外部bean作为值。(注意需要引入相应的命名空间)
<property name="teacherMap">
<map>-->
<entry key="001" value-ref="teachOne"></entry>
<entry key="002" value-ref="teachTwo"></entry>
</map>
</property>
<bean id="teachOne" class="com.xiaoye.pojo.Teacher">
<property name="tid" value="001"></property>
<property name="tname" value="小夜"></property>
</bean> <bean id="teachTwo" class="com.xiaoye.pojo.Teacher">
<property name="tid" value="002"></property>
<property name="tname" value="小也"></property>
</bean>
2.2 用ref使用map类型的外部bean,在map类型bean中使用<entry>标签的key属性设置键,用value-ref属性引用外部bean作为值。
<!--也可-->
<property name="teacherMap" ref="teacherMap"></property>
<util:map id="teacherMap">
<entry key="1001" value-ref="teachOne"></entry>
<entry key="1001" value-ref="teachTwo"></entry>
</util:map>
<bean id="teachOne" class="com.xiaoye.pojo.Teacher">
<property name="tid" value="001"></property>
<property name="tname" value="小夜"></property>
</bean> <bean id="teachTwo" class="com.xiaoye.pojo.Teacher">
<property name="tid" value="002"></property>
<property name="tname" value="小也"></property>
</bean>
注意:
使用util:list、util:map标签必须引入相应的命名空间,可以通过idea的提示功能选择
3. p命名空间
使用p命名空间后,对应简单属性可以直接赋值,对于类类型,集合类型也可以使用外部引用。
public class Student implements Person {
private Integer sid;
private String sname;
private Integer age;
private String gender;
private Clazz clazz;
private String[] hobby;
private Map<String, Teacher> teacherMap;
}
<bean id="studentSix" class="com.xiaoye.pojo.Student"
p:sid="1005" p:hobby="酒" p:clazz-ref="clazzOne" p:teacherMap-ref="teacherMap">
</bean>