1、什么是 IOC
(1)控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理
(2)使用 IOC 目的:为了耦合度降低
2、IOC 底层原理
(1)xml 解析、工厂模式、反射
IOC(BeanFactory 接口)
1、IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂
2、Spring 提供 IOC 容器实现两种方式:(两个接口)
(1)BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用
- 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
(2)ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人
员进行使用 - 加载配置文件时候就会把在配置文件对象进行创建
IOC操作bean管理
1、基于 xml 方式创建对象
(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
(2)在 bean 标签有很多属性,介绍常用的属性
- id 属性:唯一标识
- class 属性:类全路径(包类路径)
(3)创建对象时候,默认也是执行无参数构造方法完成对象创建
2、基于 xml 方式注入属性
(1)DI:依赖注入,就是注入属性
3、第一种注入方式:使用 set 方法进行注入
(1)创建类,定义属性和对应的 set 方法
package hello;
public class Book {
private String bname;
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
private String bauthor;
//set方法注入
public void setBname(String bname) {
this.bname = bname;
}
public void testDemo(){
System.out.println(bname+"::"+bauthor);
}
}
(2)在 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置User对象创建-->
<bean id="user" class="hello.User"></bean>
<!--set方法注入属性-->
<bean id="book" class="hello.Book">
<!-- 使用property完成属性注入 name属性名称 value属性注入的值-->
<property name="bname" value="三国演义"></property>
<property name="bauthor" value="罗贯中"></property>
</bean>
</beans>
第二种注入方式:
1.使用有参数构造进行注入
(1)创建类,定义属性,创建属性对应有参数构造方法
package hello;
//使用有参数构造注入
public class Orders {
private String oname;
private String address;
//有参数的构造
public Orders(String oname,String address){
this.oname=oname;
this.address=address;
}
public void ordertest(){
System.out.println(oname+""+address);
}
}
## 在 spring 配置文件中进行配置
```xml
<!-- 有参注入配置-->
<bean id="orders" class="hello.Orders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="address" value="china"></constructor-arg>
</bean>
编写测试类,测试类包含上面两种注入方法:
@Test
public void test2(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
Book book=context.getBean("book",Book.class);
System.out.println(book);
book.testDemo();
}
@Test
public void test3(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
Orders orders=context.getBean("orders",Orders.class);
System.out.println(orders);
orders.ordertest();
}
2.p 名称空间注入
(1)使用 p 名称空间注入,可以简化基于 xml 配置方式
第一步 添加 p 名称空间在配置文件中
xmlns:p="http://www.springframework.org/schema/p"
第二步 进行属性注入,在 bean 标签里面进行操作
<bean id="book" class="hello.Book" p:bname="西游记" p:bauthor="吴承恩"></bean>
测试类运行
3.
1、字面量
(1)null 值
<!--null 值--> <property name="address">
<null/>
</property>
(2)属性值包含特殊符号
<!--属性值包含特殊符号
1 把<>进行转义 < >
2 把带特殊符号内容写到 CDATA
--><property name="address">
<value><![CDATA[<<南京>>]]></value>
</property>
2、注入属性-外部 bean
(1)创建两个类 service 类和 dao 类 (2)在 service 调用 dao 里面的方法
(3)在 spring 配置文件中进行配置
service层
package service;
import dao.UserDao;
import hello.User;
public class UserService {
//创建UserDao类型属性,生成set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add......");
userDao.update();
}
}
dao,创建一个接口,并且实现这个接口
package dao;
public interface UserDao {
public void update();
}
package dao;
public class UserDaoImpl implements UserDao {
@Override
public void update() {
System.out.println("dao update......");
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- service和dao对象创建-->
<bean id="userService" class="service.UserService">
<!-- 注入UserDao对象,ref属性:创建对象bean标签id值-->
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="dao.UserDaoImpl"></bean>
</beans>
测试类的编写,并看执行效果
@Test
public void test4(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
UserService userService=context.getBean("userService",UserService.class);
userService.add();
}
3、注入属性-内部 bean
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
Dept类
package bean;
//部门类
public class Dept {
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
Emp类
package bean;
public class Emp {
private String ename;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
private String gender;
public void setDept(Dept dept) {
this.dept = dept;
}
private Dept dept;
public void add(){
System.out.println(ename+"::"+gender+"::"+dept);
}
}
配置类的编写
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 内部bean-->
<bean id="emp" class="bean.Emp">
<property name="ename" value="小子"></property>
<property name="gender" value="男"></property>
<!-- 设置对象配置类-->
<property name="dept">
<bean id="dept" class="bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
</beans>
测试类的编写
@Test
public void test5(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean3.xml");
Emp emp=context.getBean("emp",Emp.class);
emp.add();
}
4、注入属性-级联赋值
(1)第一种写法
<!--级联赋值-->
<bean id="emp" class="bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></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>
(2)第二种写法
<!--级联赋值--> <bean id="emp" class="bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean> <bean id="dept" class="bean.Dept">
<property name="dname" value="财务部"></property>
</bean>