Spring(看这一篇就够了)

在这里插入图片描述

Spring

什么是Spring

Spring是一个开源框架,为简化企业级开发而生。它以IOC(控制反转)和AOP(面向切面)为思想内核,提供了控制层SpringMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。

Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。

Spring官网地址:https://spring.io/.\

Spring体系结构

在这里插入图片描述
Spring框架根据不同的功能被划分成了多个模块,这些模块可以满足一切企业级应用开发的需求,在开发过程中可以根据需求有选择性地使用所需要的模块。

  • Core Container:Spring核心模块,任何功能的使用都离不开该模块,是其他模块建立的基础。

  • Data Access/Integration:该模块提供了数据持久化的相应功能。

  • Web:该模块提供了web开发的相应功能。

  • AOP:提供了面向切面编程实现

  • Aspects:提供与AspectJ框架的集成,该框架是一个面向切面编程框架。

  • Instrumentation:提供了类工具的支持和类加载器的实现,可以在特定的应用服务器中使用。

  • Messaging:为Spring框架集成一些基础的报文传送应用

  • dTest:提供与测试框架的集成

IOC__控制反转思想

IOC(Inversion of Control) :程序将创建对象的权利交给框架。

之前在开发过程中,对象实例的创建是由调用者管理的,代码如下

public interface StudentDao {
   
   
  // 根据id查询学生
  Student findById(int id);
}


public class StudentDaoImpl implements StudentDao{
   
   
  @Override
  public Student findById(int id) {
   
   
    // 模拟从数据库查找出学生
    return new Student(1,"zbdx","山西");
   }
}


public class StudentService {
   
   
  public Student findStudentById(int id){
   
   
    // 此处就是调用者在创建对象
    StudentDao studentDao = new StudentDaoImpl();
    return studentDao.findById(1);
   }
}

这种写法有两个缺点:

  • 浪费资源:StudentService调用方法时即会创建一个对象,如果不断调用方法则会创建大量StudentDao对象。

  • 代码耦合度高:假设随着开发,我们创建了StudentDao另一个更加完善的实现类StudentDaoImpl2,如果在StudentService中想使用StudentDaoImpl2,则必须修改源码。

而IOC思想是将创建对象的权利交给框架,框架会帮助我们创建对象,分配对象的使用,控制权由程序代码转移到了框架中,控制权发生了反转,这就是Spring的IOC思想。而IOC思想可以完美的解决以上两个问题。

自定义对象容器

在这里插入图片描述
接下来我们通过一段代码模拟IOC思想。创建一个集合容器,先将对象创建出来放到容器中,需要使用对象时,只需要从容器中获取对象即可,而不需要重新创建,此时容器就是对象的管理者。

1.创建实体类

public class Student {
   
   
  private int id;
  private String name;
  private String address;
    // 省略getter/setter/构造方法/tostring
}

2.创建Dao接口和实现类

public interface StudentDao {
   
   
  // 根据id查询学生
  Student findById(int id);
}

public class StudentDaoImpl implements StudentDao {
   
   
  @Override
  public Student findById(int id) {
   
   
    // 模拟从数据库查找出学生
    return new Student(1,"zbdx","山西");
   }
}

public class StudentDaoImpl2 implements StudentDao {
   
   
  @Override
  public Student findById(int id) {
   
   
    // 模拟从数据库查找出学生
    System.out.println("新方法!!!");
    return new Student(1,"zbdx","山西");
   }
}

3.创建配置文件bean.properties,该文件中定义管理的对象

studentDao=com.jjy.dao.impl.StudentDaoImpl

4.创建容器管理类,该类在类加载时读取配置文件,将配置文件中配置的对象全部创建并放入容器中。

public class Container {
   
   
  static Map<String,Object> map = new HashMap<>();
  static {
   
   
    // 读取配置文件
    InputStream is = Container.class.getClassLoader().getResourceAsStream("bean.properties");
    Properties properties = new Properties();
    try {
   
   
      properties.load(is);
     } catch (IOException e) {
   
   
      throw new RuntimeException(e);
     }

    // 遍历配置文件的所有配置信息
    Enumeration<Object> keys = properties.keys();
    while (keys.hasMoreElements()){
   
   
      String key = keys.nextElement().toString();
      String value = properties.getProperty(key);
      try {
   
   
        // 创建对象,将对象放入集合
        Object o = Class.forName(value).newInstance();
        map.put(key,o);
       } catch (Exception e) {
   
   
        e.printStackTrace();
       }
     }

   }

  // 从容器中获取对象
  public static Object getBean(String key){
   
   
    return map.get(key);
   }
}

创建Dao对象的调用者StudentService

public class StudentService {
   
   
  public Student findStudentById(int id){
   
   
    StudentDao studentDao = (StudentDao) Container.getBean("studentDao");
    System.out.println(studentDao.hashCode());
    return studentDao.findById(id);
   }
}

测试StudentService

public class Test {
   
   
  public static void main(String[] args) {
   
   
    StudentService studentService = new StudentService();
    System.out.println(studentService.findStudentById(1));
    System.out.println(studentService.findStudentById(1));
   }
}

测试结果:

  1. StudentService从容器中每次拿到的都是同一个StudentDao对象,节约了资源。
  2. 如果想使用StudentDaoImpl2对象,只需要修改bean.properties的内容为
    studentDao=com.itbaizhan.dao.StudentDaoImpl2
    即可,无需修改Java源码。

Spring实现IOC

接下来我们使用Spring实现IOC,Spring内部也有一个容器用来管理对象。

1.创建Maven工程,引入依赖

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.0.11</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
</dependencies>

2.创建POJO类、Dao类和接口

public class Student {
   
   
  private int id;
  private String name;
  private String address;
    // 省略getter/setter/构造方法/tostring
}

public interface StudentDao {
   
   
  // 根据id查询学生
  Student findById(int id);
}

public class StudentDaoImpl implements StudentDao{
   
   
  @Override
  public Student findById(int id) {
   
   
    // 模拟从数据库查找出学生
    return new Student(1,"zbdx","山西");
   }
}

3.编写xml配置文件,配置文件中配置需要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">

  <bean id="studentDao" class="com.jjy.dao.StudentDaoImpl"></bean>
  
</beans>

4.测试从Spring容器中获取对象。

public class TestContainer {
   
   
  @Test
  public void t1(){
   
   
    // 创建Spring容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    // 从容器获取对象
    StudentDao studentDao1 = (StudentDao) ac.getBean("studentDao");
    StudentDao studentDao2 = (StudentDao) ac.getBean("studentDao");
    System.out.println(studentDao1.hashCode());
    System.out.println(studentDao2.hashCode());
    System.out.println(studentDao1.findById(1));
   }
}

Spring容器类型

在这里插入图片描述

容器接口

  • BeanFactory:BeanFactory是Spring容器中的顶层接口,它可以对Bean对象进行管理。

  • ApplicationContext:ApplicationContext是BeanFactory的子接口。它除了继承 BeanFactory的所有功能外,还添加了对国际化、资源访问、事件传播等方面的良好支持。

ApplicationContext有以下三个常用实现类:

容器实现类

  • ClassPathXmlApplicationContext:该类可以从项目中读取配置文件

  • FileSystemXmlApplicationContext:该类从磁盘中读取配置文件

  • AnnotationConfigApplicationContext:使用该类不读取配置文件,而是会读取注解

@Test
public void t2(){
   
   
  // 创建spring容器
  //     ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\Users\\a\\IdeaProjects\\spring_demo\\src\\main\\resources\\bean.xml");
  
  // 从容器中获取对象
  StudentDao userDao = (StudentDao) ac.getBean("studentDao");
  System.out.println(userDao);
  System.out.println(userDao.findById(1));
}

对象的创建方式

Spring会帮助我们创建bean,那么它底层是调用什么方法进行创建的呢?

使用构造方法

Spring默认使用类的空参构造方法创建bean:

// 假如类没有空参构造方法,将无法完成bean的创建
public class StudentDaoImpl implements StudentDao{
   
   
  public StudentDaoImpl(int a){
   
   }
  
  @Override
  public Student findById(int id) {
   
   
    // 模拟根据id查询学生
    return new Student(1,"zbdx","山西");
   }
}

使用工厂类的方法
Spring可以调用工厂类的方法创建bean:

1.创建工厂类,工厂类提供创建对象的方法:

public class StudentDaoFactory {
   
   
  public StudentDao getStudentDao(){
   
   
    return new StudentDaoImpl(1);
   }
}

2.在配置文件中配置创建bean的方式为工厂方式。

<!--  id:工厂对象的id,class:工厂类  -->
<bean id="studentDaoFactory" class="com.jjy.dao.StudentDaoFactory"></bean>
<!--  id:bean对象的id,factory-bean:工厂对象的id,factory-method:工厂方法 -->
<bean id="studentDao" factory-bean="studentDaoFactory" factory-method="getStudentDao"></bean>

使用工厂类的静态方法
Spring可以调用工厂类的静态方法创建bean:

1.创建工厂类,工厂提供创建对象的静态方法。

public class StudentDaoFactory2 {
   
   
  public static StudentDao getStudentDao2() {
   
   
    return new StudentDao  
1Impl();
   }
}

2.在配置文件中配置创建bean的方式为工厂静态方法。

<!-- id:bean的id  class:工厂全类名 factory-method:工厂静态方法  -->
<bean id="studentDao" class="com.jjy.dao.StudentDaoFactory2" factory-method="getStudentDao2"></bean>

对象的创建策略

Spring通过配置<bean>中的scope属性设置对象的创建策略,共有五种创建策略:

  • singleton:单例,默认策略。整个项目只会创建一个对象,通过<bean>中的lazy-init属性可以设置单例对象的创建时机:

lazy-init=“false”(默认):立即创建,在容器启动时会创建配置文件中的所有Bean对象。

lazy-init=“true”:延迟创建,第一次使用Bean对象时才会创建。

配置单例策略:

<!-- <bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2" scope="singleton" lazy-init="true"></bean> -->
<bean id="studentDao" class="com.jjy.dao.StudentDaoImpl2" scope="singleton" lazy-init="false"></bean>

测试单例策略:

// 为Bean对象的类添加构造方法
public StudentDaoImpl2(){
   
   
  System.out.println("创建StudentDao!!!");
}


@Test
public void t2(){
   
   
  // 创建Spring容器
  ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
  // 从容器获取对象
  StudentDao studentDao1 = (StudentDao) ac.getBean("studentDao");
  StudentDao studentDao2 = (StudentDao) ac.getBean("studentDao");
  StudentDao studentDao3 = (StudentDao) ac.getBean("studentDao");
  System.out.println(studentDao1.hashCode());
  System.out.println(studentDao2.hashCode());
  System.out.println(studentDao3.hashCode());
}
  • prototype:多例,每次从容器中获取时都会创建对象。
<!-- 配置多例策略 -->
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2" scope="prototype"></bean>
  • request:每次请求创建一个对象,只在web环境有效。

  • session:每次会话创建一个对象,只在web环境有效。

  • gloabal-session:一次集群环境的会话创建一个对象,只在web环境有效。

对象的创建策略不同,销毁时机也不同:

  • singleton:对象随着容器的销毁而销毁。
  • prototype:使用JAVA垃圾回收机制销毁对象。
  • request:当处理请求结束,bean实例将被销毁。
  • session:当HTTP Session最终被废弃的时候,bean也会被销毁掉。
  • gloabal-session:集群环境下的session销毁,bean实例也将被销毁。

对象的生命周期

Bean对象的生命周期包含创建——使用——销毁,Spring可以配置Bean对象在创建和销毁时自动执行的方法:

定义生命周期方法

public class StudentDaoImpl2 implements StudentDao{
   
   
  // 创建时自动执行的方法
  public void init(){
   
   
    System.out.println("创建StudentDao!!!");
   }

  // 销毁时自动执行的方法
  public void destory(){
   
   
    System.out.println("销毁StudentDao!!!");
   }
}

配置生命周期方法

<!-- init-method:创建对象时执行的方法  destroy-method:销毁对象时执行的方法  -->
<bean id="studentDao" class="com.jjy.dao.StudentDaoImpl2" scope="singleton"
   init-method="init" destroy-method="destory"></bean>
 

测试

@Test
public void t3(){
   
   
  // 创建Spring容器
  ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");

  // 销毁Spring容器,ClassPathXmlApplicationContext才有销毁容器的方法
  ac.close();
}

获取对象的方式

Spring有多种获取容器中对象的方式:

1.通过id/name获取
配置文件

<bean name="studentDao" class="com.jjy.dao.StudentDaoImpl2"></bean>

<bean id="studentDao" class="com.jjy.dao.StudentDaoImpl2"></bean>

获取对象

StudentDao studentDao = (StudentDao) ac.getBean("studentDao");

通过类型获取

配置文件

<bean name="studentDao" class="com.jjy.dao.StudentDaoImpl2"></bean>

获取对象

StudentDao studentDao2 = ac.getBean(StudentDao.class);

可以看到使用类型获取不需要强转。

2.通过类型+id/name获取
虽然使用类型获取不需要强转,但如果在容器中有一个接口的多个实现类对象,则获取时会报错,此时需要使用类型+id/name获取

配置文件


<bean name="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2"></bean>
<bean name="studentDao1" class="com.itbaizhan.dao.StudentDaoImpl"></bean>

获取对象

StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);

DI_依赖注入

什么是依赖注入

依赖注入(Dependency Injection,简称DI),它是Spring控制反转思想的具体实现。

控制反转将对象的创建交给了Spring,但是对象中可能会依赖其他对象。比如service类中要有dao类的属性,我们称service依赖于dao。之前需要手动注入属性值,代码如下:

public interface StudentDao {
   
   
  Student findById(int id);
}


public class StudentDaoImpl implements StudentDao{
   
   
  @Override
  public Student findById(int id) {
   
   
    // 模拟根据id查询学生
    return new Student(1,"jjy","北京");
   }
}


public class StudentService {
   
   
  // service依赖dao,手动注入属性值,即手动维护依赖关系
  private StudentDao studentDao = new StudentDaoImpl();


  public Student findStudentById(int id){
   
   
    return studentDao.findById(id);
   }
}

此时,当StudentService的想要使用StudentDao的另一个实现类如StudentDaoImpl2时,则需要修改Java源码,造成代码的可维护性降低。

而使用Spring框架后,Spring管理Service对象与Dao对象,此时它能够为Service对象注入依赖的Dao属性值。这就是Spring的依赖注入。简单来说,控制反转是创建对象,依赖注入是为对象的属性赋值。

Setter注入

在之前开发中,我们可以通过setter方法或构造方法设置对象属性值:

// setter方法设置属性
StudentService studentService = new StudentService();
StudentDao studentDao = new StudentDao();
studentService.setStudentDao(studentDao);


// 构造方法设置属性
StudentDao studentDao = new StudentDao();
StudentService studentService = new StudentService(studentDao);

Spring可以通过调用setter方法或构造方法给属性赋值

被注入类编写属性的setter方法

public class StudentService {
   
   
  private StudentDao studentDao;


  public void setStudentDao(StudentDao studentDao) {
   
   
    this.studentDao = studentDao;
   }


  public Student findStudentById(int id){
   
   
    return studentDao.findById(id);
   }
}

配置文件中,给需要注入属性值的中设置

<bean id="studentDao" class="com.jjy.dao.StudentDaoImpl"></bean>
<bean id="studentService" class="com.jjy.service.StudentService">
  <!-- 依赖注入 name:对象的属性名 ref:容器中对象的ID值-->
  <property name="studentDao" ref="studentDao"></property>
</bean>

测试是否注入成功

@Test
public void testDI1(){
   
   
  ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  StudentService studentService = ac.getBean(StudentService.class);
  System.out.println(studentService.findStudentById(1));
}

构造方法注入

被注入类编写有参的

Tomcat是一款免费的Web服务器,专为Java Web应用程序而设计。它是一个开源项目,由Apache软件基金会开发和维护。Tomcat支持Java Servlet、JavaServer Pages(JSP)、Java Expression Language(EL)和Java WebSocket等技术。 Tomcat的最新版本是Tomcat 9,它是我们使用的最新Java Servlet API版本。Tomcat 9也支持HTTP/2协议和WebSocket 1.1规范。Tomcat 也有其他版本,比如Tomcat 8.5,它也是一个很常用的版本。 Tomcat的好处是它轻量级且易于使用。Tomcat虽然是一款Web服务器,但它可以作为嵌入式Web容器,使用Spring框架等等进行开发。由于Tomcat使用Java语言,因此Tomcat不仅适用于多个操作系统,如Windows、Linux和Mac OS X,而且还适用于多个开发平台,如Eclipse和NetBeans。 Tomcat也很容易配置。我们可以使用命令行界面轻松地配置服务器,在Tomcat的安装页面可以选择安装到什么目录,配置好后,我们可以通过在浏览器中输入localhost:8080,直接访问Tomcat Server并查看Tomcat欢迎页。接着,我们可以部署自己的Java Web应用程序,这些应用程序可以使用Java Servlet、JSP和其他Java Web技术。 总的来说,Tomcat是一款优秀的Java Web服务器,它可以快速安装和配置,适用于多个开发平台和操作系统,并且易于使用和集成。无论开发Java Web应用程序还是部署Java Web应用程序,Tomcat都是一个值得使用的工具。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中北萌新程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值