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. JAVA运算符及实例

    JAVA语言支持以下运算符 优先级() 算数运算符:+,-,*,/,%,++,-- 实例1:  package operator; ​ public class Demo01 {     public ...

  2. 赶上AI的大潮:在VSCode中使用DeepSeek编程的极简方法

    1 赶上AI的大潮:在VSCode中使用DeepSeek编程的极简方法 1.1 背景   DeepSeek在春节期间突然大行其道,欣喜国力大增的同时,对于普通IT工作者,如何才能享受这一波AI红利,让 ...

  3. Nginx - [01] 概述

    客户端访问增加.并发量增大. 001 || 什么是Nginx Nginx是一个高性能的HTTP和反向代理Web服务器,同时也提供了IMAP/POP3/SMTP服务. 占有内存少,并发能力强. 002 ...

  4. csharp入门经典

    C#简介 .NET Framework是Microsoft为开发应用程序而创建的一个具有革命意义的平台,它有运行在其他操作系统上的版本 .NET Framework的设计方式确保它可以用于各种语言,包 ...

  5. C# USB 摄像头 OpenCV 视频picBox呈现,抓拍图像保存呈现。

    1.winform 应用程序,两个picturebox空间,一个用于视频呈现,一个用于抓拍呈现. 2.引用包OpenCvSharp4.OpenCvSharp4.Extensions.OpenCvSha ...

  6. python 二级 语言基本元素笔记-字符串

    l='12345' 1.递增顺序: 正向递增从0开始 负向从[-1]开始,l[1]=2,l[-1]=5 2.切片操作:左开右闭,l[2:5]=3,4 3.导入库,引入 库名.函数名 input函数 输 ...

  7. 【练习回顾】dfs迷宫+路径打印

    很直接的dfs.递归+栈--不知道以后会不会生疏 进入一次dfs,相当于走一步,入栈:结束一次dfs,相当于这一步考虑结束,出栈 笑死,y1竟然是一个函数 突然发现写的有点槽,可以把dfs形式化为&q ...

  8. 【BUG】鸿蒙模拟器虚拟化问题的解决方案

    创建记事本文档.txt,键入以下代码: pushd "%~dp0" dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum > ...

  9. 关闭windows计划重启

    前言 windows 总是自动计划更新 解决方案 需要禁用服务 "Windows Update" 和 "更新 Orchestrator 服务" 首先去这里下载P ...

  10. mac brew 安装

    Homebrew国内源 知乎文章地址:https://zhuanlan.zhihu.com/p/111014448 苹果电脑安装脚本: /bin/zsh -c "$(curl -fsSL h ...