1.IOC的概念
控制反转,把对象创建和对象之间的调用过程,交给Sprng进行管理
使用IOC的目的:为了降低耦合度
2.IOC底层原理
底层用到三部分:xml解析、工厂模式、反射
3.IOC操作Bean管理
-
什么是Bean管理
Bean管理指的是两个操作:Spring创建对象和Spring注入属性
-
Bean管理操作有两种方式
基于xml配置文件方式实现
1、 基于xml方式创建对象
<!-- 配置User对象创建-->
<bean id="order" class="com.ljx.test.Orders">
(1)在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
(2)在bean标签有很多属性,介绍常用的属性
*id属性:唯一标识
*class属性:类全路径(包类路径)
(3)创建对象时候,默认也是执行午餐构造方法完成对象创建
2、基于xml方式注入属性
(1)DI:依赖注入,就是注入属性
3、第一种注入方式:使用set方法进行注入
(1)创建类,定义属性和对应的set方法
public class Book {
private String bookName;
private String bookAuthor;
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public void testDemo(){
System.out.println("bookName:"+bookName);
System.out.println("bookAuthor:"+bookAuthor);
}
}
(2)在Spring配置文件配置对象创建,配置属性注入
<bean id="book" class="com.ljx.test.Book">
<property name="bookName" value="水浒传">
</property>
<property name="bookAuthor" value="张三">
</property>
<!--使用 property 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
</bean>
(4)第二种注入方式:使用有参数构造进行注入
(1)创建类,定义属性,创建属性对应有参数构造方式
public class Orders {
private String name;
private String address;
public Orders(String name, String address) {
this.name = name;
this.address = address;
}
public void testOrder(){
System.out.println("name:"+name);
System.out.println("address:"+address);
}
}
(2)在Spring配置文件中进行配置
<bean id="order" class="com.ljx.test.Orders">
<constructor-arg name="name" value="书籍"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
5、P名称空间注入(了解)
(1)使用P名称空间注入,可以简化基于xml配置方式
第一步 添加P名称空间在配置文件中
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
第二步 进行属性注入,在bean标签里面进行操作
<!--set方法注入属性-->
<bean id="book" class="com.ljx.test.Book" p:bookName="水浒传" p:bookAuthor="张三"></bean>
6、通过xml注入其他类型属性
(1)null值
<property name="address">
<null/>
</property>
(2)含有其他符号的属性值
<property name="address">
<value> <!CDATA[<<南京>>]></value>
<property>
7、通过xml注入属性-外部bean
(1)创建两个类service和dao类
package com.ljx.test.service;
import com.ljx.test.Dao.UserDao;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
userDao.update();
System.out.println("add............");
}
}
package com.ljx.test.Dao;
public interface UserDao {
public void update();
}
package com.ljx.test.Dao.Impl;
import com.ljx.test.Dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void update() {
System.out.println("update...........");
}
}
(2)在service调用dao里面的方法
(3)在spring配置文件中进行配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- service和dao的对象创建-->
<bean id="UserService" class="com.ljx.test.service.UserService">
<property name="UserDao" ref="UserDaoImpl">
<!-- 注入UserDao对象
name:属性名
ref:创建UserDao对象时bean标签的id属性值
-->
</property>
</bean>
<bean id="UserDaoImpl" class="com.ljx.test.Dao.Impl.UserDaoImpl"></bean>
</beans>
8.注入集合属性
(1)创建一个类声明集合属性
package com.learn.spring5.collectiontype;
import javax.sound.midi.Soundbank;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
//注入数组属性
private String[] course;
//注入list集合属性
private List<String> list;
//注入map集合属性
private Map<String,String> map;
//注入set集合属性
private Set<String> set;
public void setCourse(String[] course) {
this.course = course;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void add(){
System.out.println(Arrays.toString(course));
System.out.println(list);
System.out.println(map);
System.out.println(set);
}
}
(2)在Spring配置文件中配置
<!-- 集合属性注入-->
<bean id="Stu" class="com.learn.spring5.collectiontype.Stu">
<!-- 注入数组类型属性(可以使用array标签和list标签)-->
<property name="course">
<array>
<value>Java</value>
<value>Pyhton</value>
</array>
</property>
<!-- 注入list类型属性(使用list标签)-->
<property name="list">
<list>
<value>99</value>
<value>100</value>
</list>
</property>
<!-- 注入map类型属性(使用map标签,在map标签里面使用entry标签来存键值对)-->
<property name="map">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PYTHON" value="Python"></entry>
</map>
</property>
<!-- 注入set类型属性(使用set标签)-->
<property name="set">
<set>
<value>李四</value>
<value>张三</value>
</set>
</property>
</bean>
(3)注入list对象集合
<property name="courses">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
<!-- 创建多个课程对象-->
<bean id="course1" class="com.learn.spring5.collectiontype.Course">
<property name="cname" value="乾坤大挪移"></property>
</bean>
<bean id="course2" class="com.learn.spring5.collectiontype.Course">
<property name="cname" value="降龙十八掌"></property>
</bean>
(4)提取list集合注入
使用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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/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">
</beans>
提取list集合属性注入
<!-- 提取list集合类型注入-->
<util:list id="bookList">
<value>易筋经</value>
<value>九阳神功</value>
<value>降龙十八掌</value>
</util:list>
<bean id="book" class="com.learn.spring5.collectiontype.Book">
<property name="name" ref="bookList"></property>
</bean>