Spring框架是一个容器,是整合其他框架的框架
他的核心是IOC(控制反转)和AOP(面向切面编程),由20多个模块构成,在很多领域都提供了优秀的问题解决方案
示例分析:在开发中普遍需要使用到日志输出功能,会将日志输出功能大量耦合到项目的不同位置,如上图左侧所示。
而日志输出功能与项目本身的核心业务逻辑无关,我们只是为了不时的查看程序的运行状态。则可以将日志功能单独提出去开发,在需要的地方将日志输出功能(所谓:切面)反织回去即可,如上图右侧所示。
- //程序员创建对象
- Student stu = new Student();
- //程序员进行赋值
- stu.setName("荷包蛋");
- stu.setAge(20);
"stu" class="com.example.pojo.Student"> -
-
"name" value="荷包蛋" /> -
"age" value="20" />
- <!-- 添加spring依赖-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.3.22</version>
- </dependency>
- package com.example.pojo;
-
- public class Student {
- private String name;
- private int age;
-
- public Student() {
- System.out.println("无参方法被调用,Student实例被创建.....");
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
- "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.example.pojo.Student"/>
- beans>
- package com.example.test;
-
- import com.example.pojo.Student;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestStudent {
-
- //测试程序员手动创建实例对象
- @Test
- public void testStudent(){
- Student stu = new Student();
- System.out.println(stu);
- }
-
- //测试Spring容器创建实例对象
- @Test
- public void testStudentSpring(){
- //创建Spring容器,并启动
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- //从容器中获取对象
- Student stu = (Student) applicationContext.getBean("stu");
- System.out.println(stu);
- }
- }
- 无参方法被调用,Student实例被创建.....
- Student{name='null', age=0}
-
- Process finished with exit code 0
- @Test
- public void testStudentSpring(){
- //创建Spring容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("source01/applicationContext.xml");
- }
- 无参方法被调用,Student实例被创建.....
-
- Process finished with exit code 0
- public void setName(String name) {
- this.name = name;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- <bean id="stu" class="com.example.pojo.Student">
- <property name="name" value="荷包蛋"/>
- <property name="age" value="20"/>
- </bean>
- @Test
- public void testStudentSpring(){
- //创建Spring容器,并启动
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("source01/applicationContext.xml");
- //从容器中获取对象
- Student stu = (Student) applicationContext.getBean("stu");
- System.out.println(stu);
- }
- 无参方法被调用,Student实例被创建.....
- Student{name='荷包蛋', age=20}
-
- Process finished with exit code 0
- private String name;
- private String address;
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- public School() {
- System.out.println("School类的构造方法被执行,实体对象被创建.....");
- }
-
- @Override
- public String toString() {
- return "School{" +
- "name='" + name + '\'' +
- ", address='" + address + '\'' +
- '}';
- }
- private String name;
- private int age;
- private School school;
-
- public Student() {
- System.out.println("Student类的构造方法执行,实体对象被创建....");
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public void setSchool(School school) {
- this.school = school;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", school=" + school +
- '}';
- }
- <!-- 定义School实体类的实例对象-->
- <bean id="school" class="com.example.pojo02.School">
- <property name="name" value="nefu"/>
- <property name="address" value="哈尔滨"/>
- </bean>
-
- <!-- 定义Student实体类的实例对象 -->
- <bean id="stu" class="com.example.pojo02.Student">
- <property name="name" value="荷包蛋"/>
- <property name="age" value="20"/>
- <!-- 根据bean工厂中注册过的对象,进行依赖注入 -->
- <property name="school" ref="school"/>
- </bean>
-
- <!--
- 对于Student对象持有的School对象的引用
- 根据bean工厂中注册过的对象(不分注册先后),进行依赖注入
- (待注入属性类型必须和已经注册过的对象的类型一致,才可进行依赖注入)
- -->
- @Test
- public void testStudent(){
- //创建Spring容器,同时生成bean工厂中注册的对象
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("source02/applicationContext.xml");
- //获取对象
- Student stu = (Student) applicationContext.getBean("stu");
- System.out.println(stu);
- }
- School类的构造方法被执行,实体对象被创建.....
- Student类的构造方法执行,实体对象被创建....
- Student{name='荷包蛋', age=20, school=School{name='nefu', address='哈尔滨'}}
-
- Process finished with exit code 0