九点钟☆方向

记录成长点滴

欢迎来到我的个人博客~


Spring笔记(一) Spring IOC

目录

Spring概述

什么是Spring

Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IOC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 SpringMVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的 Java EE 企业应用开源框架。

Spring的优势

  1. 方便解耦,简化开发
  2. AOP 编程的支持
  3. 声明式事务的支持
  4. 方便程序的测试
  5. 方便集成各种优秀框架

IOC的概念和作用

IOC:控制反转〈Inversion of Control)把创建对象的权利交给框架。

作用:削减程序间的耦合性(解除我们代码中的依赖关系)

Spring的IOC入门案例

基于xml配置IOC步骤

  1. 引入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    
  2. 业务层接口及实现类

    public interface IAccountService {
        /**
        * 保存账户
        */
        void saveAccount();
    }
       
       
    public class AccountServiceImpl implements IAccountService {
        private IAccountDao accountDao = new AccountDaoImpl();
           
        @Override
        public void saveAccount() {
        	accountDao.saveAccount();
        }
    }
    
  3. 创建持久层接口和实现类

    public interface IAccountDao {
        /**
        * 保存账户
        */
        void saveAccount();
    }
       
       
    public class AccountDaoImpl implements IAccountDao {
        @Override
        public void saveAccount() {
        	System.out.println("保存了账户");
    	}
    }
    
  4. 创建spring配置文件:可以是任意名称(但不能是中文)。如bean.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">
        <!-- 配置 service -->
        <bean id="accountService" class="com.mg.service.impl.AccountServiceImpl"></bean>
        <!-- 配置 dao -->
        <bean id="accountDao" class="com.mg.dao.impl.AccountDaoImpl"></bean>
    </beans>
       
       
    bean 标签:用于配置让 spring 创建对象,并且存入 ioc 容器之中。默认情况下它调用的是类中的无参构造函数。
    		  如果没有无参构造函数则不能创建成功。
        id 属性:给对象在容器中提供一个唯一标识。用于获取对象
        class 属性:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数
    
  5. 使用测试

    @Test
    public void test1() {
        //1.使用 ApplicationContext 接口,就是在获取 spring 容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据配置文件中bean 的 id 获取对象
        IAccountService accountService = (IAccountService) ac.getBean("accountService");
        //这种方式和上面的强转效果一致
        //IAccountDao dao = ac.getBean("accountDao",IAccountDao.class);
        System.out.println("service------"+accountService);
    	System.out.println("dao-------"+dao);
    }
    

基于xml配置IOC的细节

spring 中工厂的类结构图

1

BeanFactory 和 ApplicationContext 的区别

BeanFactory 才是 Spring 容器中的顶层接口。ApplicationContext 是它的子接口。

区别在于创建对象的时间点不一样:

ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。 BeanFactory:什么使用什么时候创建对象。

ApplicationContext 接口的实现类

ClassPathXmlApplicationContext:它是从类的根路径下加载配置文件 推荐使用这种

FileSystemXmlApplicationContext:它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置

AnnotationConfigApplicationContext: 当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。

IOC 中 bean 标签和管理对象细节

创建bean的三种方式

  1. 使用默认无参构造函数。它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。

    <bean id="accountService" class="com.mg.service.impl.AccountServiceImpl"/>
    
  2. 使用普通工厂的方法创建对象(使用某个类中的方法创建对象,并存入Spring容器)

    //模拟一个工厂类,该类可能位于jar包中,我们无法通过修改源码方式提供默认构造函数
    //此工厂创建对象,必须现有工厂实例对象,再调用方法
    public class InstanceFactory {
        public IAccountService createAccountService(){
        	return new AccountServiceImpl();
        }
    }
    
    <!-- 此种方式是:先把工厂的创建交给 spring 来管理。然后在使用工厂的 bean 来调用里面的方法
    factory-bean 属性:用于指定实例工厂 bean 的 id。
    factory-method 属性:用于指定实例工厂中创建对象的方法。
    -->
    <bean id="instancFactory" class="com.mg.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instancFactory"
    	  factory-method="createAccountService">
    </bean>
    
  3. 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入Spring容器)

    //模拟一个静态工厂,该类可能位于jar包中,我们无法通过修改源码方式提供默认构造函数
    public class StaticFactory {
        public static IAccountService createAccountService(){
        	return new AccountServiceImpl();
        }
    }
    
    <!-- 此种方式是:使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器
    id 属性:指定 bean 的 id,用于从容器中获取
    class 属性:指定静态工厂的全限定类名
    factory-method 属性:指定生产对象的静态方法
    -->
    <bean id="accountService" class="com.mg.factory.StaticFactory"
    	  factory-method="createAccountService">
    </bean>
    

bean的作用范围

使用scope属性指定

  1. singleton:默认值,单例的.
  2. prototype:多例的.
  3. request:WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
  4. session:WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
  5. global session:WEB项目中应用在集群环境的会话范围。如果没有集群环境那么相当于 session
<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl" scope="prototype"/>

bean的生命周期

  1. 单例对象: scope=”singleton” 一个应用只有一个对象的实例。它的作用范围就是整个引用。 生命周期: 对象出生:当应用加载,创建容器时,对象就被创建了。 对象活着:只要容器在,对象一直活着。 对象死亡:当应用卸载,销毁容器时,对象就被销毁了。

    总结:单例对象的声明周期和容器相同

  2. 多例对象: scope=”prototype” 每次访问对象时,都会重新创建对象实例。 生命周期: 对象出生:当使用对象时,创建新的对象实例。 对象活着:只要对象在使用中,就一直活着。 对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。

bean标签属性补充

init-method:指定类中的初始化方法名称

destroy-method: 指定类中销毁方法名称

public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao = new AccountDaoImpl();

    public void save() {
        accountDao.save();
    }

    //初始化方法
    public void init() {
        System.out.println("初始化方法");
    }

    //销毁方法
    public void destory() {
        System.out.println("销毁方法");
    }
}
<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl" scope="singleton" 
      init-method="init" destroy-method="destory">
</bean>

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦