spring Ioc笔记
spring:是一个专门创建及管理bean对象的开源框架
1、IOC:控制反转
就是控制权的转移,把原来需要开发人员自己创建维护的对象交给Spring容器管理的过程
DI:依赖注入
spring容器获得组件指定,去容器中查找需要的Bean,通过注入方式注入到某个属性中
注入方式:1、set方法 2、构造方法 3、注解注入
注入类型:1、值注入 2、地址注入
scope属性 生命周期属性
1.single 默认值
单利对象:被标识为单利的对象在Spring容器中只会存在一个实例
2.prototype 多例
多利对象:每一次都会创建新的对象
3.request 会话
在web环境下,对象与页面的request生命周期一致
4.session 会话
在web环境下,对象与页面的session生命周期一致
lazy懒加载
只有在第一次被使用的时候才去加载或创建
spring Ioc思维图
#创建spring项目
Set注入
第一步、导入依赖/jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.2</version>
</dependency>
第二步,编写一个实体类拥有测试
package entity;
public class Student {
private Integer id;
private Integer age;
private String name;
private Integer height;
//提供get/set 方法 和toString方法方便测试
public Integer getId() {
return id;
}
//无参构造器
public Student() {
}
//构造注入 有参构造器
public Student(Integer id, Integer age, String name, Integer height) {
this.id = id;
this.age = age;
this.name = name;
this.height = height;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
", height=" + height +
'}';
}
}
第三步,新建一个spring.xml文件
<?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">
<!-- set注入 即通过get/set方法注入-->
<bean id="Stu" class="entity.Student"> <!--这里的创建bean id值随意起名字,但一个spring的id不能出现多个,为唯一值 -->
<!--创建bean想当于new 对象-->
<property name="id" value="3"></property> <!--这里是name就是实体类的属性值,要于属性值一致 value则就是给id赋值-->
<property name="age" value="18"></property> <!--想当于 setAge(18)-->
<property name="name" value="魏无羡"></property>
<property name="height" value="180"></property>
</bean>
<!--set注入完成-->
</beans>
第四步测试
package Test;
import entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test01 {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); //扫描xml文件
Student student = (Student) context.getBean("Stu"); //调用applicationContext.xml容器中bean标签id为Stu的
System.out.println(student.toString());
}
}
执行结果
spring 的 构造注入
先给实体类添加构造方法,并重写无参构造,因为写了有参构造,默认无参构造器就失去了作用,防止出现错误,需重写无参构造
在spring的xml文件中创建bean
<?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">
<!-- set注入 即通过get/set方法注入-->
<bean id="Stu" class="entity.Student"> <!--这里的创建bean id值随意起名字,但一个spring的id不能出现多个,为唯一值 -->
<!--创建bean想当于new 对象-->
<property name="id" value="3"></property> <!--这里是name就是实体类的属性值,要于属性值一致 value则就是给id赋值-->
<property name="age" value="18"></property> <!--想当于 setAge(18)-->
<property name="name" value="魏无羡"></property>
<property name="height" value="180"></property>
</bean>
<!--set注入完成-->
<!--构造方法注入-->
<!--通过有参构造函数对属性进行赋值-->
<!--同一个类可以创建多个bean-->
<!--构造方法不在是property了而是constructor-arg看得懂英文的都知道扫描意思了-->
<bean id="Stu2" class="entity.Student">
<constructor-arg name="id" value="12"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="name" value="蓝湛"></constructor-arg>
<constructor-arg name="height" value="190"></constructor-arg>
</bean>
<!--构造方法注入完成,去Test类找蓝湛-->
</beans>
Test类测试构造注入
spring的xml的ref映射
新建一个service类并写一个方法
package service;
import entity.Student;
import java.lang.reflect.Method;
public class StudentService {
private Student student;
private Student student2;
//通过构造方法注入
public StudentService(Student student ,Student student2) {
this.student = student;
this.student2 = student2;
}
public StudentService() {
}
public void method(){
System.out.println(student.getName()+"和"+student2.getName()+"一起到云深不知处喝酒");
}
}
编写xml文件
<?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">
<!-- set注入 即通过get/set方法注入-->
<bean id="Stu" class="entity.Student"> <!--这里的创建bean id值随意起名字,但一个spring的id不能出现多个,为唯一值 -->
<!--创建bean想当于new 对象-->
<property name="id" value="3"></property> <!--这里是name就是实体类的属性值,要于属性值一致 value则就是给id赋值-->
<property name="age" value="18"></property> <!--想当于 setAge(18)-->
<property name="name" value="魏无羡"></property>
<property name="height" value="180"></property>
</bean>
<!--set注入完成-->
<!--构造方法注入-->
<!--通过有参构造函数对属性进行赋值-->
<!--同一个类可以创建多个bean-->
<!--构造方法不在是property了而是constructor-arg看得懂英文的都知道扫描意思了-->
<bean id="Stu2" class="entity.Student">
<constructor-arg name="id" value="12"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="name" value="蓝湛"></constructor-arg>
<constructor-arg name="height" value="190"></constructor-arg>
</bean>
<!--构造方法注入完成,去Test类找蓝湛-->
<!--bean文件的ref映射,即创建对象时,把其他对象一起创建,前提是这个对象是他本身的属性-->
<!--并通过构造方法进行实例 通过构造方法进行注入-->
<bean id="StuService" class="service.StudentService">
<constructor-arg name="student" ref="Stu"></constructor-arg>
<constructor-arg name="student2" ref="Stu2"></constructor-arg>
</bean>
<!--ref映射到的就是bean的id值,就相当于和bean中的对象进行实例化了,并拥有他的属性-->
</beans>
Test类
package Test;
import entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;
public class test01 {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); //扫描xml文件
//set注入验证
Student student = (Student) context.getBean("Stu"); //调用applicationContext.xml容器中bean标签id为Stu的
System.out.println("这是set注入");
System.out.println(student.toString());
//构造注入验证
Student stu = (Student) context.getBean("Stu2");
System.out.println("这是构造注入");
System.out.println(stu.toString());
//bean容器的ref映射
StudentService service = (StudentService) context.getBean("StuService");
System.out.println("\n");
System.out.println("这是ref映射");
service.method();
}
}
测试结果
结论:
这就是spring Ioc中的xml文件的使用方式了,可以看得到,我们全程没有去new这个对象,完全的交给了xml文件去实例化,这就是我们所说的控制反转了,把我们new对象的主动权交给了sping去实例,减少了耦合,当我们改变了名字或者其他操作时,只需要在xml文件进行更改即可,不需要到每个类去进行更改,这就很大程度进行了解耦。