Spring对Bean的管理细节

目录

- 项目结构
- 创建bean的三种方式
- bean对象的作用范围
- bean对象的生命周期

项目结构

在这里插入图片描述

  • AccountServiceImpl.java
package com.itheima.service.impl;

import com.itheima.service.IAccountService;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    public AccountServiceImpl(){
        System.out.println("AccountService创建了。");
    }

    public void saveAccount(){
        System.out.println("service中的saveAccount執行了。");
    }

}
  • IAccountService.java
package com.itheima.service;

/**
 * 账户业务层的接口
 */
public interface IAccountService {
    /**
     * 模拟保存账户
     */
    void saveAccount();
}
  • Client.java
package com.itheima.ui;

import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    /**
     * 获取spring的IOC核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取bean对象
        IAccountService as = (IAccountService) ac.getBean("accountService");

        as.saveAccount();


    }

}
  • 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">
    <!--把对象的创建交给spring来管理-->
    <!--Spring对bean的管理细节
        1、创建bean的三种方式
        2、bean对象的作用范围
        3、bean对象的生命周期-->

    <!--创建bean的三种方式-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

</beans>

创建bean的三种方式

  • 第一种方式:使用默认构造函数创建。
    在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果没有默认构造函数,则对象创建无法创建。
  • 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">
    <!--把对象的创建交给spring来管理-->
    <!--Spring对bean的管理细节
        1、创建bean的三种方式
        2、bean对象的作用范围
        3、bean对象的生命周期-->

    <!--创建bean的三种方式-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

</beans>
  • AccountServiceImpl.java
/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {
    //没有重写默认构造函数
    public AccountServiceImpl(String name){
        System.out.println("AccountService创建了。");
    }

    public void saveAccount(){
        System.out.println("service中的saveAccount執行了。");
    }

}

报错:Failed to instantiate: No default constructor.
在这里插入图片描述

  • 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)。
  • bean.xml
<!--第二种方式-->
    <bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
  • InstanceFactory.java
/**
 * 模拟一个工厂类,该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数
 */
public class InstanceFactory {
     public IAccountService getAccountService(){
         return new AccountServiceImpl();
     }

}
  • 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入容器)。
  • bean.xml
<!--第三种方式-->
    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
  • StaticFactory.java
/**
 * 模拟一个工厂类,该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数
 */
public class StaticFactory {
     public static IAccountService getAccountService(){
         return new AccountServiceImpl();
     }

}

在jar包中的方法,希望通过spring容器来获取,可以使用第二种和第三种方式。

bean对象的作用范围

工厂模式解耦的博客中,我们说过spring的bean一般都是单例模式的bean。
Spring的bean对象默认情况下是单例的。

  • bean标签的scope属性

    用于指定bean的作用范围。
    取值:
    - singleton
    - prototype
    - request
    - session
    - global-session

    • singleton
      单例,默认值。
    • prototpye
      多例
    • request
      作用于web应用的请求范围
    • session
      作用于web应用的会话范围
    • global-session
      作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session。在负载均衡中,验证码需要使用global-session,因为登录请求和输入验证码请求可能会被分发到不同的服务器上。

bean对象的生命周期

  • 单例对象

    出生:当容器创建时,对象出生
    活着:只要容器存在,对象一直活着
    死亡:容器销毁,对象消亡
    总结:单例对象的生命周期和容器相同

    • bean.xml
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>
    
    • AccountServiceImpl.java
/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    public AccountServiceImpl(){
        System.out.println("AccountService创建了。");
    }

    public void saveAccount(){
        System.out.println("service中的saveAccount執行了。");
    }

    public void init(){
        System.out.println("service中的saveAccount初始化了。");
    }

    public void destory(){
        System.out.println("service中的saveAccount销毁了。");
    }

}
  • Client.java
/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    /**
     * 获取spring的IOC核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取bean对象
        IAccountService as = (IAccountService) ac.getBean("accountService");

        as.saveAccount();
        //手动销毁容器
        ((ClassPathXmlApplicationContext) ac).close();
    }

}
  • 多例对象
    出生:当使用对象时,spring框架为我们创建对象
    活着:对象在使用过程中一直活着
    死亡:当对象长时间不用,且没有别的对象引用时,由java的垃圾回收器进行回收
  • bean.xml
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype" init-method="init" destroy-method="destory"></bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值