56.md
###企业级开发框架 - Spring Framework 是整个 Spring 生态的基础,各个模块都是基于 Spring Framework 衍生出来的。 - Spring Boot 是一个快速开发框架,让开发者可以快速搭建一套基于 Spring 的应用程序,集成了 Spring 其他模块以及第三方模块,MyBatis、Hibernate 等,只需简单的配置就可以使用,开箱即用,默认支持 JSON 格式,实现前后端开发非常方便,Spring Boot + Vue。 - Spring Cloud 是一套整合了分布式应用常用模块的框架,基于 Spring Boot。 Maven:软件管理工具 POM:Project Object Model,就是一个 XML 文件,配置 jar 依赖关系。 ### Spring 应用程序 1、创建 Maven 工程。 2、pom.xml 中引入 Spring 依赖。 ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.7.RELEASE</version> </dependency> </dependencies> ``` Spring 的两大核心机制:IoC(控制反转) 和 AOP(面向切面编程) IoC 对象创建不再由开发者完成,而是容器自动创建,开发者直接取出来用即可。 3、创建 spring.xml ```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="stu" class="com.southwind.entity.Student" > <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="22"></property> </bean> </beans> ``` 4、加载 IoC 容器,获取创建好的 bean。 ```java public class IoCTest { public static void main(String[] args) { //1.加载 IoC 容器,spring.xml ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); Student student = (Student) applicationContext.getBean("stu"); System.out.println(student); } } ``` 特殊字符的处理方式。 ```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="stu" class="com.southwind.entity.Student" > <property name="id" value="1"></property> <!-- <property name="name" value="<张三>"></property>--> <property name="name"> <value><![CDATA[<张三>]]></value> </property> <property name="age" value="22"></property> </bean> </beans> ``` 使用 IoC 容器创建对象,实体类的注意事项: - 必须有无参构造函数。 - 成员变量必须有 setter 方法。 IoC 实例化对象的过程,通过反射+XML解析的方式对 spring.xml 进行处理,反射拿到无参构造函数,调用创建对象,同时获取 setter 方法,调用完成成员变量的赋值。 ### 获取 IoC bean 的方式 - 通过 id 获取 ```java Student student = (Student) applicationContext.getBean("stu"); ``` - 通过运行时类获取 ```java Student student = applicationContext.getBean(Student.class); ``` 通过运行时类获取 bean 存在一个弊端,当 spring.xml 中配置两个 Student 的 bean 时会抛出异常。 ### 创建 IoC bean 的方式 - 无参构造 ```xml <bean id="stu2" class="com.southwind.entity.Student"> <property name="id" value="2"></property> <property name="name" value="李四"></property> <property name="age" value="23"></property> </bean> ``` - 有参构造 1、在实体类中创建有参构造。 ```java public Student(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } ``` 2、在 spring.xml 中进行配置。 ```xml <bean id="stu3" class="com.southwind.entity.Student"> <constructor-arg index="1" value="王五"></constructor-arg> <constructor-arg index="0" value="1"></constructor-arg> <constructor-arg index="2" value="20"></constructor-arg> </bean> ``` ```xml <bean id="stu3" class="com.southwind.entity.Student"> <constructor-arg name="name" value="王五"></constructor-arg> <constructor-arg name="id" value="1"></constructor-arg> <constructor-arg name="age" value="20"></constructor-arg> </bean> ``` ```xml <bean id="stu3" class="com.southwind.entity.Student"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="王五"></constructor-arg> <constructor-arg value="20"></constructor-arg> </bean> ``` ### 两个 bean 的级联 ```java package com.southwind.entity; public class Student { private Integer id; private String name; private Integer age; private Classes classes; public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", classes=" + classes + '}'; } public Student(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Student() { } } ``` ```java package com.southwind.entity; public class Classes { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Classes{" + "id=" + id + ", name='" + name + '\'' + '}'; } } ``` ```xml <bean id="stu2" class="com.southwind.entity.Student"> <property name="id" value="2"></property> <property name="name" value="李四"></property> <property name="age" value="23"></property> <property name="classes" ref="classes1"></property> </bean> <bean id="classes1" class="com.southwind.entity.Classes"> <property name="id" value="1"></property> <property name="name" value="Java班"></property> </bean> ```
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.southwind</groupId> <artifactId>56</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.7.RELEASE</version> </dependency> </dependencies> </project>
Classes.java
package com.southwind.entity; public class Classes { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Classes{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
Student.java
package com.southwind.entity; public class Student { private Integer id; private String name; private Integer age; private Classes classes; public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", classes=" + classes + '}'; } public Student(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Student() { } }
IoCTest.java
package com.southwind.test; import com.southwind.entity.Classes; import com.southwind.entity.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IoCTest { public static void main(String[] args) { //1.加载 IoC 容器,spring.xml ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); // Student student = (Student) applicationContext.getBean("stu"); // Student student = applicationContext.getBean(Student.class); // System.out.println(student); Student student = (Student) applicationContext.getBean("stu2"); System.out.println(student); Classes classes = (Classes) applicationContext.getBean("classes1"); System.out.println(classes); } }
Test.java
package com.southwind.test; import com.southwind.entity.Student; public class Test { public static void main(String[] args) { // Student student = new Student(); // student.setId(1); // student.setName("张三"); // student.setAge(22); // System.out.println(student); } }
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"> <!-- <bean id="stu" class="com.southwind.entity.Student" >--> <!-- <property name="id" value="1"></property>--> <!--<!– <property name="name" value="<张三>"></property>–>--> <!-- <property name="name">--> <!-- <value><![CDATA[<张三>]]></value>--> <!-- </property>--> <!-- <property name="age" value="22"></property>--> <!-- </bean>--> <bean id="stu2" class="com.southwind.entity.Student"> <property name="id" value="2"></property> <property name="name" value="李四"></property> <property name="age" value="23"></property> <property name="classes" ref="classes1"></property> </bean> <!-- <bean id="stu3" class="com.southwind.entity.Student">--> <!-- <constructor-arg index="1" value="王五"></constructor-arg>--> <!-- <constructor-arg index="0" value="1"></constructor-arg>--> <!-- <constructor-arg index="2" value="20"></constructor-arg>--> <!-- </bean>--> <bean id="classes1" class="com.southwind.entity.Classes"> <property name="id" value="1"></property> <property name="name" value="Java班"></property> </bean> </beans>