Spring创建Bean的三种方式及Bean的生命周期

Spring创建Bean的三种方式

第一种方式:使用默认构造函数创建

在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。

采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。

Spring配置文件

<bean id="accountServiceOne" class="com.zjw.service.impl.AccountServiceOneImpl" />

Java类

package com.zjw.service.impl;

import com.zjw.service.IAccountServiceOne;

/**
* 账户的业务层实现类
* 对象创建的三种方式一:通过构造方法创建对象
*/
public class AccountServiceOneImpl implements IAccountServiceOne { public AccountServiceOneImpl() {
System.out.println("AccountServiceOneImpl……我创建了。。");
} @Override
public void saveAccount() {
System.out.println("AccountServiceOneImpl中的saveAccount方法执行了");
}
}

测试


/**
* 模拟一个表现层,用于调用业务层
*/
public class Client { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //对象创建的三种方式一:通过构造方法创建对象
IAccountServiceOne accountServiceOne = (IAccountServiceOne) ac.getBean("accountServiceOne");
System.out.println(accountServiceOne);
accountServiceOne.saveAccount(); System.out.println("main 方法结束了。。。。"); }
}

结果

AccountServiceOneImpl……我创建了。。。
com.zjw.service.impl.AccountServiceOneImpl@258e2e41
AccountServiceOneImpl中的saveAccount方法执行了
main 方法结束了。。。。

第二种方式:使用普通工厂中的方法创建对象

使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)

Spring配置

    <!--第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)-->
<bean id="instanceFactory" class="com.zjw.factory.InstanceFactory"/>
<bean id="accountServiceTwo" factory-bean="instanceFactory" factory-method="getAccountService" />

工厂类

/**
* @author zjw
*/
public class InstanceFactory { public IAccountServiceTwo getAccountService(){
return new AccountServiceTwoImpl();
}
}

Java类

/**
* 账户的业务层实现类
* 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
*/
public class AccountServiceTwoImpl implements IAccountServiceTwo { public AccountServiceTwoImpl() {
System.out.println("AccountServiceTwoImpl……我创建了。。");
} @Override
public void saveAccount() {
System.out.println("AccountServiceTwoImpl中的saveAccount方法执行了");
}
}

测试

/**
* 模拟一个表现层,用于调用业务层
*/
public class Client { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //第二种方式:使用普通工厂中的方法创建对象
IAccountServiceTwo accountServiceTwo = ac.getBean("accountServiceTwo", IAccountServiceTwo.class);
System.out.println(accountServiceTwo);
accountServiceTwo.saveAccount(); System.out.println("main 方法结束了。。。。");
}
}

结果

AccountServiceTwoImpl……我创建了。。
com.zjw.service.impl.AccountServiceTwoImpl@258e2e41
AccountServiceTwoImpl中的saveAccount方法执行了
main 方法结束了。。。。

第三种方式:使用工厂中的静态方法创建对象

Spring配置

    <!--第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring)-->
<bean id="accountServiceThree" class="com.zjw.factory.StaticFactory" factory-method="getAccountService" />

工厂类


public class StaticFactory {
public static IAccountServiceThree getAccountService(){
return new AccountServiceThreeImpl();
}
}

Java类

/**
* 账户的业务层实现类
* 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring)
*/
public class AccountServiceThreeImpl implements IAccountServiceThree { public AccountServiceThreeImpl() {
System.out.println("AccountServiceThreeImpl……我创建了。。");
} @Override
public void saveAccount() {
System.out.println("AccountServiceThreeImpl中的saveAccount方法执行了");
}
}

测试

/**
* 模拟一个表现层,用于调用业务层
*/
public class Client { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //第二种方式:使用工厂中的静态方法创建对象
IAccountServiceThree accountServiceThree= ac.getBean("accountServiceThree", IAccountServiceThree.class);
System.out.println(accountServiceThree);
accountServiceThree.saveAccount(); System.out.println("main 方法结束了。。。。"); }
}

结果

AccountServiceThreeImpl……我创建了。。
com.zjw.service.impl.AccountServiceThreeImpl@258e2e41
AccountServiceThreeImpl中的saveAccount方法执行了
main 方法结束了。。。。

Bean的生命周期

  • 单例对象

出生:当容器创建时对象出生

活着:只要容器还在,对象一直活着

死亡:容器销毁,对象给你消亡

总结:单例对象的生命周期和容器相同

  • 多例对象

出生:当我们使用对象时spring框架为我们创建

活着:对象只要是在使用过程中就一直活着

死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收

Spring配置

    <!--
init-method:对象创建后执行的方法
destroy-method:对象销毁时执行的方法
单例对象,容器关闭后执行
多例模式,java虚拟机决定
scope:单例还是多例对象
singleton:默认,单例
prototype:多例
-->
<bean id="accountService" class="com.zjw.service.impl.AccountServiceImpl"
init-method="init" destroy-method="destroy" />

Java类

/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService { public AccountServiceImpl() {
System.out.println("AccountServiceImpl……我创建了。。");
} @Override
public void saveAccount() {
System.out.println("AccountServiceImpl……saveAccount方法执行了");
} public void init(){
System.out.println("AccountServiceImpl……init方法执行了。。");
} public void destroy(){
System.out.println("AccountServiceImpl……destroy方法执行了。。");
}
}

测试

/**
* 模拟一个表现层,用于调用业务层
*/
public class Client { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //Bean的生命周期
IAccountService accountService = (IAccountService) ac.getBean("accountService");
System.out.println(accountService);
accountService.saveAccount();
System.out.println(ac.isSingleton("accountService")); ac.close();
System.out.println(accountService);
System.out.println("main 方法结束了。。。。");
}
}

结果

AccountServiceImpl……我创建了。。
AccountServiceImpl……init方法执行了。。
com.zjw.service.impl.AccountServiceImpl@add0edd
AccountServiceImpl……saveAccount方法执行了
true
AccountServiceImpl……destroy方法执行了。。
com.zjw.service.impl.AccountServiceImpl@add0edd
main 方法结束了。。。。

Spring创建Bean的三种方式及Bean的生命周期的更多相关文章

  1. Spring实例化Bean的三种方式及Bean的类型

    1.使用类构造器实例化  [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...

  2. spring创建bean的三种方式

    spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...

  3. Spring学习之实例化bean的三种方式

    实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...

  4. Spring创建JobDetail的两种方式

    一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路径. 2.编 ...

  5. Spring 循环依赖的三种方式(三级缓存解决Set循环依赖问题)

    本篇文章解决以下问题: [1] . Spring循环依赖指的是什么? [2] . Spring能解决哪种情况的循环依赖?不能解决哪种情况? [3] . Spring能解决的循环依赖原理(三级缓存) 一 ...

  6. spring生成EntityManagerFactory的三种方式

    spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...

  7. Spring 使用AspectJ的三种方式

    Spring 使用AspectJ 的三种方式 一,使用JavaConfig 二,使用注解隐式配置 三,使用XML 配置 背景知识: 注意 使用AspectJ 的 时候 要导入相应的Jar 包 嗯 昨天 ...

  8. Spring静态注入的三种方式

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...

  9. 0036 Java学习笔记-多线程-创建线程的三种方式

    创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...

  10. js学习-DOM之动态创建元素的三种方式、插入元素、onkeydown与onkeyup两个事件整理

    动态创建元素的三种方式: 第一种: Document.write(); <body> <input type="button" id="btn" ...

随机推荐

  1. initDB.sh初始化磁盘脚本centos7

    新加磁盘初始化脚本 跳转:优化(2022-4-14) vim initDB.sh #!/bin/bash # auther by wangxp EXCLUDE_LIST='2,11' EXCLUDE_ ...

  2. Irwin-Hall 分布/CF1477F 题解

    Irwin-Hall 分布 对于 \(n\) 个均匀分布于 \([0,1]\) 的连续随机变量 \(X_1,X_2,\dots,X_n\),其和的随机变量 \(X\) 满足: \[P(X\le x)= ...

  3. Clickhouse常见异常

    一.异常 1)DB::Exception: Nested type Array(String) cannot be inside Nullable type (version 20.4.6.53 (o ...

  4. Dev Express WPF 在当前界面显示进度加载等待信息

    执行耗时任务时,为提高用户体验,一般会添加进度状态信息.Dev Express 的 LoadingDecorator 可以实现在当前界面中显示进度信息. 效果图如下: 默认 LoadingDecora ...

  5. C# DataTable 类使用

    命名空间: System.Data 程序集: System.Data.Common.dll 参考连接:https://docs.microsoft.com/zh-cn/dotnet/api/syste ...

  6. Nginx 拒绝错误SNI请求以防止源站被扫描

    Nginx 1.19.4 版本更新了一个新的配置,允许使用 ssl_reject_handshake 这个参数来拒绝错误 SNI 请求的握手,可以防止被类似Censys互联网扫码工具扫描出源站ip 在 ...

  7. Ansible - [01] 入门&安装部署

    自动化运维工具,可以批量远程其他主机并进行管理操作 一.什么是 Ansible Ansible首次发布于2012年,作者:Michael DeHaan,同时也是Cobbler的作者,Ansible于2 ...

  8. Linux - [-bash: jps: command not found]

    jps是jdk提供的一个查看当前java进程的小工具, 全称是 JavaVirtual Machine Process Status Tool $ jps [options] [hostid] opt ...

  9. MD5 - windows也可以查询某个文件的MD5码

    命令格式 certutil -hashfile 文件名称 md5 示例 Microsoft Windows [版本 10.0.22621.1702] (c) Microsoft Corporation ...

  10. 三分钟掌握音视频处理 | 在 Rust 中优雅地使用 FFmpeg

    前言 音视频处理看似高深莫测,但在开发中,我们或多或少都会遇到相关需求,比如视频格式转换.剪辑.添加水印.音频提取等. FFmpeg 作为行业标准,几乎无所不能,很多流行的软件(如 VLC.YouTu ...