一、IOC容器
1、什么是IOC(控制反转)
a)把对象创建和对象之间的调用过程,交给Spring进行管理
b)使用IOC目的:为了降低耦合度
2、IOC底层
a)xml解析、工厂模式、反射
3、Spring提供的IOC容器实现的两种方式(两个接口)
a)BeanFactory接口:IOC容器基本实现是Spring内部接口的使用接口,不提供给开发人员进行使用(加载配置文件时候不会创建对象,在获取对象时才会创建对象。)
b)ApplicationContext接口:BeanFactory接口的子接口,提供更多更强大的功能,提供给开发人员使用(加载配置文件时候就会把在配置文件对象进行创建)推荐使用!
4、ApplicationContext接口的实现类(具体根据API文档查看☺)
二、IOC容器-Bean管理
1、IOC操作Bean管理
a)Bean管理就是两个操作:(1)Spring创建对象;(2)Spring注入属性
2、基于XML配置文件创建对象
1
2
|
<!--1 配置User对象创建--> < bean id = "user" class = "com.atguigu.spring5.User" ></ bean > |
3、基于XML方式注入属性(DI:依赖注入(注入属性))
a)set方式注入
1
2
3
4
5
6
7
8
9
10
|
//(1)传统方式: 创建类,定义属性和对应的set方法 public class Book { //创建属性 private String bname; //创建属性对应的set方法 public void setBname(String bname) { this .bname = bname; } } |
1
2
3
4
5
6
7
8
9
|
<!--(2)spring方式: set方法注入属性--> < bean id = "book" class = "com.atguigu.spring5.Book" > <!--使用property完成属性注入 name:类里面属性名称 value:向属性注入的值 --> < property name = "bname" value = "Hello" ></ property > < property name = "bauthor" value = "World" ></ property > </ bean > |
b)有参构造函数注入
1
2
3
4
5
6
7
8
9
10
11
|
//(1)传统方式:创建类,构建有参函数 public class Orders { //属性 private String oname; private String address; //有参数构造 public Orders(String oname,String address) { this .oname = oname; this .address = address; } } |
1
2
3
4
5
|
<!--(2)spring方式:有参数构造注入属性--> < bean id = "orders" class = "com.atguigu.spring5.Orders" > < constructor-arg name = "oname" value = "Hello" ></ constructor-arg > < constructor-arg name = "address" value = "China!" ></ constructor-arg > </ bean > |
c)p名称空间注入(了解即可)
1
2
3
4
5
6
7
8
9
|
<!--1、添加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" <!--在这里添加一行p--> <!--2、在bean标签进行属性注入(算是set方式注入的简化操作)--> < bean id = "book" class = "com.atguigu.spring5.Book" p:bname = "very" p:bauthor = "good" > </ bean > |
4、注入空值和特殊符号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< bean id = "book" class = "com.atguigu.spring5.Book" > <!--(1)null值--> < property name = "address" > < null /> <!--属性里边添加一个null标签--> </ property > <!--(2)特殊符号赋值--> <!--属性值包含特殊符号 a 把<>进行转义 < > b 把带特殊符号内容写到CDATA --> < property name = "address" > < value > <![CDATA[<<南京>>]]> </ value > </ property > </ bean > |
5、注入属性-外部bean
a)创建两个类service和dao类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class UserService { //service类 //创建UserDao类型属性,生成set方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this .userDao = userDao; } public void add() { System.out.println( "service add..............." ); userDao.update(); //调用dao方法 } } public class UserDaoImpl implements UserDao { //dao类 @Override public void update() { System.out.println( "dao update..........." ); } } |
b)在spring配置文件中进行配置
1
2
3
4
5
6
7
8
9
|
<!--1 service和dao对象创建--> < bean id = "userService" class = "com.atguigu.spring5.service.UserService" > <!--注入userDao对象 name属性:类里面属性名称 ref属性:创建userDao对象bean标签id值 --> < property name = "userDao" ref = "userDaoImpl" ></ property > </ bean > < bean id = "userDaoImpl" class = "com.atguigu.spring5.dao.UserDaoImpl" ></ bean > |
6、基于XML方式注入内部bean和级联赋值
a)注入属性-内部bean
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门(部门是一,员工是多)
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//部门类 public class Dept { private String dname; public void setDname(String dname) { this .dname = dname; } } //员工类 public class Emp { private String ename; private String gender; //员工属于某一个部门,使用对象形式表示 private Dept dept; public void setDept(Dept dept) { this .dept = dept; } public void setEname(String ename) { this .ename = ename; } public void setGender(String gender) { this .gender = gender; } } |
(3)在spring配置文件中配置
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--内部bean--> < bean id = "emp" class = "com.atguigu.spring5.bean.Emp" > <!--设置两个普通属性--> < property name = "ename" value = "Andy" ></ property > < property name = "gender" value = "女" ></ property > <!--设置对象类型属性--> < property name = "dept" > < bean id = "dept" class = "com.atguigu.spring5.bean.Dept" > <!--内部bean赋值--> < property name = "dname" value = "宣传部门" ></ property > </ bean > </ property > </ bean > |
b)注入属性-级联赋值
1
2
3
4
5
6
7
8
9
10
11
|
<!--方式一:级联赋值--> < bean id = "emp" class = "com.atguigu.spring5.bean.Emp" > <!--设置两个普通属性--> < property name = "ename" value = "Andy" ></ property > < property name = "gender" value = "女" ></ property > <!--级联赋值--> < property name = "dept" ref = "dept" ></ property > </ bean > < bean id = "dept" class = "com.atguigu.spring5.bean.Dept" > < property name = "dname" value = "公关部门" ></ property > </ bean > |
1
2
3
4
|
//方式二:生成dept的get方法(get方法必须有!!) public Dept getDept() { return dept; } |
1
2
3
4
5
6
7
8
9
10
11
|
<!--级联赋值--> < bean id = "emp" class = "com.atguigu.spring5.bean.Emp" > <!--设置两个普通属性--> < property name = "ename" value = "jams" ></ property > < property name = "gender" value = "男" ></ property > <!--级联赋值--> < property name = "dept" ref = "dept" ></ property > < property name = "dept.dname" value = "技术部门" ></ property > </ bean > < bean id = "dept" class = "com.atguigu.spring5.bean.Dept" > </ bean > |
7、IOC 操作 Bean 管理——xml 注入集合属性
1、注入数组类型属性 2、注入 List 集合类型属性 3、注入 Map 集合类型属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法 public class Stu { //1 数组类型属性 private String[] courses; //2 list集合类型属性 private List<String> list; //3 map集合类型属性 private Map<String,String> maps; //4 set集合类型属性 private Set<String> sets; public void setSets(Set<String> sets) { this .sets = sets; } public void setCourses(String[] courses) { this .courses = courses; } public void setList(List<String> list) { this .list = list; } public void setMaps(Map<String, String> maps) { this .maps = maps; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!--(2)在 spring 配置文件进行配置--> < bean id = "stu" class = "com.atguigu.spring5.collectiontype.Stu" > <!--数组类型属性注入--> < property name = "courses" > < array > < value >java课程</ value > < value >数据库课程</ value > </ array > </ property > <!--list类型属性注入--> < property name = "list" > < list > < value >张三</ value > < value >小三</ value > </ list > </ property > <!--map类型属性注入--> < property name = "maps" > < map > < entry key = "JAVA" value = "java" ></ entry > < entry key = "PHP" value = "php" ></ entry > </ map > </ property > <!--set类型属性注入--> < property name = "sets" > < set > < value >MySQL</ value > < value >Redis</ value > </ set > </ property > </ bean > |
8、在集合里面设置对象类型值
1
2
3
4
5
|
//学生所学多门课程 private List<Course> courseList; //创建集合 public void setCourseList(List<Course> courseList) { this .courseList = courseList; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!--创建多个course对象--> < bean id = "course1" class = "com.atguigu.spring5.collectiontype.Course" > < property name = "cname" value = "Spring5框架" ></ property > </ bean > < bean id = "course2" class = "com.atguigu.spring5.collectiontype.Course" > < property name = "cname" value = "MyBatis框架" ></ property > </ bean > <!--注入list集合类型,值是对象--> < property name = "courseList" > < list > < ref bean = "course1" ></ ref > < ref bean = "course2" ></ ref > </ list > </ property > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!--第一步:在 spring 配置文件中引入名称空间 util--> <? 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" <!--添加util名称空间--> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--添加util名称空间--> <!--第二步:使用 util 标签完成 list 集合注入提取--> <!--把集合注入部分提取出来--> <!--1 提取list集合类型属性注入--> < util:list id = "bookList" > < value >易筋经</ value > < value >九阴真经</ value > < value >九阳神功</ value > </ util:list > <!--2 提取list集合类型属性注入使用--> < bean id = "book" class = "com.atguigu.spring5.collectiontype.Book" scope = "prototype" > < property name = "list" ref = "bookList" ></ property > </ bean > |
总结
到此这篇关于spring IOC容器管理必须知道的一些操作的文章就介绍到这了,更多相关spring IOC容器管理操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_45496190/article/details/107059038