目录
(5)对象构造函数方式的IOC注入赋值——3种方式(名称+索引+顺序——>按对象属性类型此处不演示)
本章学习源码Github地址(setter方式):https://github.com/GuiZhouAndroid/MySpringAllProject/tree/master/SpringDemo01_IOC_SET_XML
本章学习源码Github地址(构造函数方式):https://github.com/GuiZhouAndroid/MySpringAllProject/tree/master/SpringDemo04_Constructor_IOC_XML
一、Spring框架概念
Spring框架是一个容器,整合其它框架的框架。它的核心是IOC和AOP,它由20多个模块构成,它在很多领域都提供优秀的解决方案。
(1)面向接口编程
使用接口,就是面向灵活,项目的可扩展性,可维护性都极高。接口不关心实现类的类型,使用时接口指向实现类,切换实现类即可切换整个功能。
(2)面向切面编程——AOP
将公共、通用、重复的代码单独开发,在需要时反织回去,底层的原理是动态代理。
(3)什么控制正转?
由程序员进行对象的创建和依赖注入称为正转——程序员说了算。
Student stu = new Student(); // ===>程序员创建对象
stu.setName("张松"); // ===>程序员进行赋值
stu.setAge(24); // ===>程序员进行赋值
(4)什么是控制反转——IOC?
IOC(Inversion of Control)控制反转是一个概念,是一种思想。由Spring容器创建对象和依赖注入,将控制权从程序员手中夺走,程序员在使用时直接取出使用——容器说了算。
<bean id="stu" class="com.dhrj.java.zsking.pojo.Student">
<property name="name" value="张松"></property>
<property name="age" value="24"></property>
</bean>
二、Spring框架使用
(1)添加依赖——pom.xml
<!-- 依赖清单 -->
<dependencies>
<!-- 单元代码测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring依赖配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
</dependencies>
(2)pom.xml中<build>构建配置——resources
<!-- 项目构建配置 -->
<build>
<resources>
<!-- 扫描加载java目录下指定后缀资源 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<!-- 扫描加载resources目录下指定后缀资源 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
(3)XML方式的IOC——简单类型注入值
步骤1:resources创建子目录(可选)——>app1(可选)——右击app1目录(可选)——>新建Spring配置的约束XML文件——>命名为applicationContext.xml——>回车——(配置文件必须要在resources目录下)
<?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">
<!-- 创建各种需要容器管理的对象 -->
</beans>
步骤2:配置文件完成,然后新增Student学生实体类,简单体验基于XML方式下IOC自动创建管理学生对象以及给学生对象完成自动注入的属性赋值。Student类如下:
public class Student {
private String name;
private int age;
//交给Spring容器注入值,必须提供setXXX()方法
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
System.out.println("Spring创建学生对象的无参构造执行了...");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
步骤3:修改Spring配置文件——applicationContext.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">
<!--创建学生对象等同于 Student stu = new Student();
id:就是创建的对象的名称
class:就是创建的对象的类型,底层通过反射构建对象
启动容器的同时,创建对象
-->
<bean id="myStu" class="com.dhrj.java.zsking.pojo.Student">
<!-- Student类若没有提供setXXX()方法,"name"和"age"会提示报红,意味着无法使用Spring框架给名为"myStu"的学生对象注入赋值"张松"和"24" -->
<property name="name" value="张松"></property>
<property name="age" value="24"></property>
</bean>
</beans>
步骤4:使用junit测试IOC
/**
* resources/app1/applicationContext.xml下的id为“myStu”的Spring注入
*/
@Test
public void testStudentSpring() {
//想从spring容器中取出对象,则要先创建容器对象,并启动才可以取对象.
ApplicationContext ac = new ClassPathXmlApplicationContext("app1/applicationContext.xml");
//取出学生对象
Student stu = (Student) ac.getBean("myStu");
System.out.println(stu);
}
运行结果:(IOC创建对象成功+IOC管理对象成功(自动注入))
(4)XML方式的ref属性——引用类型注入值
步骤1:创建Book书实体类
public class Book {
private String bookName;//书名
private int bookPrice;//售价
public Book() {
System.out.println("Spring创建书对象的无参构造执行了......");
}
//交给Spring容器注入值,必须提供setXXX()方法
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setBookPrice(int bookPrice) {
this.bookPrice = bookPrice;
}
@Override
public String toString() {
return "Book{" +
"bookName='" + bookName + '\'' +
", bookPrice=" + bookPrice +
'}';
}
}
步骤2:创建StudentAndBook学生的书实体类
public class StudentAndBook {
private String name;//姓名
private int age;//年龄
//引用类型的成员变量
private Book book;//学生的书
public StudentAndBook() {
System.out.println("学生的书无参构造方法执行了...");
}
//交给Spring容器注入值,必须提供setXXX()方法
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "StudentAndBook{" +
"name='" + name + '\'' +
", age=" + age +
", book=" + book +
'}';
}
}
步骤3:创建Spring配置文件——resources创建子目录app2——右击app2目录——>新建Spring配置的约束XML文件——>命名为applicationContext.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">
<!--创建书对象-->
<bean id="myBook" class="com.dhrj.java.zsking.pojo.Book">
<property name="bookName" value="露娜月下无限连"></property>
<property name="bookPrice" value="666"></property>
</bean>
<!--创建学生和书对象-->
<bean id="myStuAndBook" class="com.dhrj.java.zsking.pojo.StudentAndBook">
<property name="name" value="张松"></property>
<property name="age" value="24"></property>
<property name="book" ref="myBook"></property>
</bean>
</beans>
步骤4:使用junit测试IOC
/**
* app2/applicationContext.xml下的id为“myBook”的Spring注入
*/
@Test
public void testBookSpring() {
ApplicationContext ac = new ClassPathXmlApplicationContext("app2/applicationContext.xml");
//取出书对象
Book book = (Book) ac.getBean("myBook");
System.out.println(book);
}
/**
* app2/applicationContext.xml下的id为“myStuAndBook”的Spring注入
*/
@Test
public void testStudentAndBookSpring() {
ApplicationContext ac = new ClassPathXmlApplicationContext("app2/applicationContext.xml");
//取出学生和书对象
StudentAndBook studentAndBook = (StudentAndBook) ac.getBean("myStuAndBook");
System.out.println(studentAndBook);
}
运行结果:
(5)对象构造函数方式的IOC注入赋值——3种方式(名称+索引+顺序——>按对象属性类型此处不演示)
步骤1:新建School学校类,增添有参构造覆盖无参构造——确保没有setXXX()方法。
public class School {
private String sName;
private String sAddress;
//没有提供setXXX()方法
//使用带参构造方法通过Spring容器注入值到IOC创建的实例对象————>注意:覆盖无参构造方法
public School(String sName, String sAddress) {
this.sName = sName;
this.sAddress = sAddress;
}
@Override
public String toString() {
return "School{" +
"sName='" + sName + '\'' +
", sAddress='" + sAddress + '\'' +
'}';
}
}
步骤2:新建Student学生类,增添有参构造覆盖无参构造——确保没有setXXX()方法。
public class Student {
private String name;
private int age;
//引用类型School
private School school;
//不提供setter()方法,提供全参构造方法,给Spring给实例对象注入初始值
public Student(String name, int age, School school) {
this.name = name;
this.age = age;
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
步骤3:新建Spring配置文件——>applicationContext.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">
<!-- 1.创建学校对象,并使用构造方法的参数名称进行注入值-->
<bean id="mySchoolByParam" class="com.dhrj.java.zsitking.pojo.School">
<constructor-arg name="sName" value="六盘水师范学院"></constructor-arg>
<constructor-arg name="sAddress" value="贵州省六盘水市明湖公园"></constructor-arg>
</bean>
<!-- 2.创建学生对象,并使用构造方法参数的下标注入值-->
<bean id="myStudentByIndex" class="com.dhrj.java.zsitking.pojo.Student">
<constructor-arg index="0" value="张松"></constructor-arg>
<constructor-arg index="1" value="24"></constructor-arg>
<constructor-arg index="2" ref="mySchoolByParam"></constructor-arg>
</bean>
<!-- 2.创建学生对象,并使用构造方法参数的顺序注入值(不能更改constructor标签位置,影响注入结果导致报错)-->
<bean id="myStudentByParamOrder" class="com.dhrj.java.zsitking.pojo.Student">
<constructor-arg value="张松"></constructor-arg>
<constructor-arg value="24"></constructor-arg>
<constructor-arg ref="mySchoolByParam"></constructor-arg>
</bean>
</beans>
步骤4:使用junit测试IOC——>测试实体构造方法使用Spring容器注入对象值
/**
* 使用构造方法的参数名称进行注入值
*/
@Test
public void test01() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
School mySchool = (School) applicationContext.getBean("mySchoolByParam");
System.out.println(mySchool);
}
/**
* 使用构造方法参数的下标注入值
*/
@Test
public void test02() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) applicationContext.getBean("myStudentByIndex");
System.out.println(student);
}
/**
* 使用默认的构造方法的参数的顺序注入值
*/
@Test
public void test03() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) applicationContext.getBean("myStudentByParamOrder");
System.out.println(student);
}
三、总结
仅自己学习记录,如有错误,敬请谅解~,谢谢~~~